You are here

class TermsOfUseForm in Terms of Use 8

Hierarchy

Expanded class hierarchy of TermsOfUseForm

1 string reference to 'TermsOfUseForm'
terms_of_use.routing.yml in ./terms_of_use.routing.yml
terms_of_use.routing.yml

File

src/Form/TermsOfUseForm.php, line 15
Contains \Drupal\terms_of_use\Form\TermsOfUseForm.

Namespace

Drupal\terms_of_use\Form
View source
class TermsOfUseForm extends ConfigFormBase {

  /**
   * Save node title in validateForm and submitForm save it in the configuration
   * @var $node_title
   */
  protected $node_title;

  /**
   * {@inheritdoc}
   */
  public function getFormId() {
    return 'terms_of_use_form';
  }

  /**
   * {@inheritdoc}
   */

  // Autocomplete for terms_of_use_node_title doesn't work
  // terms_of_use.autocomplete is the routing configuration for the path
  // /terms_of_use/autocomplete and called \Drupal\terms_of_use\AutocompleteController
  // localizated in src/AutocompletController.php.
  // Whe a request for the path /terms_of_use/autocomplete is done, it returns
  // a Drupal 404 error
  // Try http://base_url/terms_of_use/autocomplete
  public function buildForm(array $form, FormStateInterface $form_state) {
    $config = $this
      ->config('terms_of_use.settings');

    // Adding the fieldset for node specification.
    $form['terms_of_use_text'] = array(
      '#type' => 'fieldset',
      '#prefix' => '<div id="fieldset-wrapper">',
      '#suffix' => '</div>',
    );
    $form['terms_of_use_text']['terms_of_use_node_id'] = array(
      '#type' => 'textfield',
      '#title' => $this
        ->t('Node id where your Terms of Use are published'),
      '#default_value' => $config
        ->get('node_id'),
      '#description' => $this
        ->t('Node <em>id</em> of the page or story (or blog entry or book page) where your Terms of Use are published.'),
    );

    /*$form['terms_of_use_text']['terms_of_use_node_title'] = array(
        '#type' => 'textfield',
        '#title' => $this->t('Title of the post where your Terms of Use are published'),
        '#default_value' => $config->get('node_title')),
        '#description' => $this->t('Node <em>title</em> of the page or story (or blog entry or book page) where your Terms of Use are published.'),
        '#autocomplete_route_name' => 'terms_of_use.autocomplete',
      );
      $form['terms_of_use_text']['terms_of_use_pick_node_id'] = array(
        '#type' => 'button',
        '#value' => $this->t('I prefer to specify the node id'),
        '#weight' => 10,
        '#ajax' => array(
          'callback' => '::js',
          'wrapper' => 'fieldset-wrapper',
        ),
      );*/

    // Adding the fieldset for form specification.
    $form['terms_of_use_form'] = array(
      '#type' => 'fieldset',
    );
    $form['terms_of_use_form']['terms_of_use_fieldset_name'] = array(
      '#type' => 'textfield',
      '#title' => $this
        ->t('Label for the fieldset'),
      '#default_value' => $config
        ->get('fieldset_name'),
      '#description' => $this
        ->t('The text for the Terms of Use and the [x] checkbox are contained in a fieldset. Type here the title for that fieldset.'),
    );
    $form['terms_of_use_form']['terms_of_use_checkbox_label'] = array(
      '#type' => 'textfield',
      '#title' => $this
        ->t('Label for the checkbox'),
      '#default_value' => $config
        ->get('checkbox_label'),
      '#description' => $this
        ->t('Type here something like "I agree with these terms." or "I CERTIFY THAT I AM OVER THE AGE OF 18 YEARS OLD.", without quotes. You can use the token @link to insert a link to the Terms in this label. For example, the label can be: "I agree with the @link.", without quotes. You may want to link to the Terms if you prefer not to show the full text of the Terms in the registration form. If you use the token, the Terms will not be shown.'),
    );
    return parent::buildForm($form, $form_state);
  }

  /**
   * {@inheritdoc}
   *
   * First check if a nid was set. Then check if the nid exists
   */
  public function validateForm(array &$form, FormStateInterface $form_state) {
    if (NULL !== $form_state
      ->getValue('terms_of_use_node_id')) {
      $nid = $form_state
        ->getValue('terms_of_use_node_id');
      if (empty($nid)) {
        $form_state
          ->setErrorByName('terms_of_use_node_id', $this
          ->t('You must specify a node <em>nid</em>.'));
      }
      else {
        $node = Node::load($nid);
        if ($node == NULL) {
          $form_state
            ->setErrorByName('terms_of_use_node_id', $this
            ->t('No post was published with <em>nid</em> !nid.', array(
            '!nid' => $nid,
          )));
        }
        else {
          $this->node_title = $node
            ->label();
        }
      }
    }

    /*
        elseif (!empty($form_state['values']['terms_of_use_node_title'])) {
          $nid = db_select('node', 'n')
            ->fields('n', array('nid'))
            ->condition('n.title', db_like($form_state['values']['terms_of_use_node_title']), 'LIKE')
            ->condition('n.status', 1)
            ->range(0, 1)
            ->addTag('node_access')
            ->execute()
            ->fetchField();

          if (!$nid) {
            $form_state->setErrorByName('terms_of_use_node_title', $this->t('No post was published with this title.'));
          }
          else {
            $config->set('node_id', $nid);
          }
        }
        else {
          $form_state->setErrorByName('terms_of_use_node_title', $this->t('You must specify a node title.'));
        }*/
  }

  /**
   * {@inheritdoc}
   */
  public function submitForm(array &$form, FormStateInterface $form_state) {
    parent::submitForm($form, $form_state);
    $config = $this
      ->config('terms_of_use.settings');
    $node_id = $form_state
      ->getValue('terms_of_use_node_id');
    $fieldset_name = $form_state
      ->getValue('terms_of_use_fieldset_name');
    $checkbox_label = $form_state
      ->getValue('terms_of_use_checkbox_label');
    $config
      ->set('node_id', $node_id)
      ->set('node_title', $this->node_title)
      ->set('fieldset_name', $fieldset_name)
      ->set('checkbox_label', $checkbox_label)
      ->save();
  }

  /**
   * Menu callback for AHAH addition.
   */
  public function js(array &$form, FormStateInterface $form_state) {
    $config = $this
      ->config('terms_of_use.settings');
    if (isset($form['terms_of_use_text']['terms_of_use_node_title'])) {

      // Create the extra field.
      $form['terms_of_use_text']['terms_of_use_node_id'] = array(
        '#type' => 'textfield',
        '#title' => $this
          ->t('Node id where your Terms of Use are published'),
        '#default_value' => $config
          ->get('node_id', ''),
        '#description' => $this
          ->t('Node <em>id</em> of the page or story (or blog entry or book page) where your Terms of Use are published.'),
      );
      unset($form['terms_of_use_text']['terms_of_use_node_title']);
      $form['terms_of_use_text']['terms_of_use_pick_node_id']['#value'] = $this
        ->t('I prefer to provide the title of the post');
    }
    else {

      // Create the extra field.
      $form['terms_of_use_text']['terms_of_use_node_title'] = array(
        '#type' => 'textfield',
        '#title' => $this
          ->t('Title of the post where your Terms of Use are published'),
        '#default_value' => $config
          ->get('node_title', ''),
        '#description' => t('Node <em>title</em> of the page or story (or blog entry or book page) where your Terms of Use are published.'),
        '#autocomplete_route_name' => 'terms_of_use.autocomplete',
      );
      unset($form['terms_of_use_text']['terms_of_use_node_id']);
      $form['terms_of_use_text']['terms_of_use_pick_node_id']['#value'] = $this
        ->t('I prefer to specify the node id');
    }

    // Return the form.
    // @todo: Resolve the bug when user click node_id button and then cannot return back to node_title field.
    return $form['terms_of_use_text'];
  }

}

Members

Namesort descending Modifiers Type Description Overrides
ConfigFormBase::create public static function Instantiates a new instance of this class. Overrides FormBase::create 13
ConfigFormBase::__construct public function Constructs a \Drupal\system\ConfigFormBase object. 11
ConfigFormBaseTrait::config protected function Retrieves a configuration object.
ConfigFormBaseTrait::getEditableConfigNames abstract protected function Gets the configuration names that will be editable. 32
DependencySerializationTrait::$_entityStorages protected property An array of entity type IDs keyed by the property name of their storages.
DependencySerializationTrait::$_serviceIds protected property An array of service IDs keyed by property name used for serialization.
DependencySerializationTrait::__sleep public function 1
DependencySerializationTrait::__wakeup public function 2
FormBase::$configFactory protected property The config factory. 1
FormBase::$requestStack protected property The request stack. 1
FormBase::$routeMatch protected property The route match.
FormBase::configFactory protected function Gets the config factory for this form. 1
FormBase::container private function Returns the service container.
FormBase::currentUser protected function Gets the current user.
FormBase::getRequest protected function Gets the request object.
FormBase::getRouteMatch protected function Gets the route match.
FormBase::logger protected function Gets the logger for a specific channel.
FormBase::redirect protected function Returns a redirect response object for the specified route. Overrides UrlGeneratorTrait::redirect
FormBase::resetConfigFactory public function Resets the configuration factory.
FormBase::setConfigFactory public function Sets the config factory for this form.
FormBase::setRequestStack public function Sets the request stack object to use.
LinkGeneratorTrait::$linkGenerator protected property The link generator. 1
LinkGeneratorTrait::getLinkGenerator Deprecated protected function Returns the link generator.
LinkGeneratorTrait::l Deprecated protected function Renders a link to a route given a route name and its parameters.
LinkGeneratorTrait::setLinkGenerator Deprecated public function Sets the link generator service.
LoggerChannelTrait::$loggerFactory protected property The logger channel factory service.
LoggerChannelTrait::getLogger protected function Gets the logger for a specific channel.
LoggerChannelTrait::setLoggerFactory public function Injects the logger channel factory.
MessengerTrait::$messenger protected property The messenger. 29
MessengerTrait::messenger public function Gets the messenger. 29
MessengerTrait::setMessenger public function Sets the messenger.
RedirectDestinationTrait::$redirectDestination protected property The redirect destination service. 1
RedirectDestinationTrait::getDestinationArray protected function Prepares a 'destination' URL query parameter for use with \Drupal\Core\Url.
RedirectDestinationTrait::getRedirectDestination protected function Returns the redirect destination service.
RedirectDestinationTrait::setRedirectDestination public function Sets the redirect destination service.
StringTranslationTrait::$stringTranslation protected property The string translation service. 1
StringTranslationTrait::formatPlural protected function Formats a string containing a count of items.
StringTranslationTrait::getNumberOfPlurals protected function Returns the number of plurals supported by a given language.
StringTranslationTrait::getStringTranslation protected function Gets the string translation service.
StringTranslationTrait::setStringTranslation public function Sets the string translation service to use. 2
StringTranslationTrait::t protected function Translates a string to the current language or to a given language.
TermsOfUseForm::$node_title protected property Save node title in validateForm and submitForm save it in the configuration
TermsOfUseForm::buildForm public function Form constructor. Overrides ConfigFormBase::buildForm
TermsOfUseForm::getFormId public function Returns a unique string identifying the form. Overrides FormInterface::getFormId
TermsOfUseForm::js public function Menu callback for AHAH addition.
TermsOfUseForm::submitForm public function Form submission handler. Overrides ConfigFormBase::submitForm
TermsOfUseForm::validateForm public function First check if a nid was set. Then check if the nid exists Overrides FormBase::validateForm
UrlGeneratorTrait::$urlGenerator protected property The url generator.
UrlGeneratorTrait::getUrlGenerator Deprecated protected function Returns the URL generator service.
UrlGeneratorTrait::setUrlGenerator Deprecated public function Sets the URL generator service.
UrlGeneratorTrait::url Deprecated protected function Generates a URL or path for a specific route based on the given parameters.