You are here

TermsOfUseForm.php in Terms of Use 8

File

src/Form/TermsOfUseForm.php
View source
<?php

/**
 * @file
 * Contains \Drupal\terms_of_use\Form\TermsOfUseForm.
 */
namespace Drupal\terms_of_use\Form;

use Drupal\Core\Form\ConfigFormBase;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Form\FormBuilderInterface;
use Drupal\node\Entity\Node;
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'];
  }

}

Classes

Namesort descending Description
TermsOfUseForm