You are here

class CategoriesForm in Frequently Asked Questions 8

Form for the FAQ settings page - categories tab.

Hierarchy

Expanded class hierarchy of CategoriesForm

File

src/Form/CategoriesForm.php, line 12

Namespace

Drupal\faq\Form
View source
class CategoriesForm extends ConfigFormBase {

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

  /**
   * {@inheritdoc}
   */
  protected function getEditableConfigNames() {
    return [];
  }

  /**
   * {@inheritdoc}
   */
  public function buildForm(array $form, FormStateInterface $form_state) {
    $faq_settings = $this
      ->config('faq.settings');

    // Set up a hidden variable.
    $form['faq_display'] = array(
      '#type' => 'hidden',
      '#value' => $faq_settings
        ->get('display'),
    );
    $form['faq_use_categories'] = array(
      '#type' => 'checkbox',
      '#title' => $this
        ->t('Categorize questions'),
      '#description' => $this
        ->t('This allows the user to display the questions according to the categories configured on the add/edit FAQ page.  Use of sub-categories is only recommended for large lists of questions.  The Taxonomy module must be enabled.'),
      '#default_value' => $faq_settings
        ->get('use_categories'),
    );
    $category_options['none'] = t("Don't display");
    $category_options['categories_inline'] = $this
      ->t('Categories inline');
    $category_options['hide_qa'] = $this
      ->t('Clicking on category opens/hides questions and answers under category');
    $category_options['new_page'] = $this
      ->t('Clicking on category opens the questions/answers in a new page');
    $form['faq_category_display'] = array(
      '#type' => 'radios',
      '#options' => $category_options,
      '#title' => $this
        ->t('Categories layout'),
      '#description' => $this
        ->t('This controls how the categories are displayed on the page and what happens when someone clicks on the category.'),
      '#default_value' => $faq_settings
        ->get('category_display'),
    );
    $form['faq_category_misc'] = array(
      '#type' => 'details',
      '#title' => $this
        ->t('Miscellaneous layout settings'),
      '#open' => TRUE,
    );
    $form['faq_category_misc']['faq_category_listing'] = array(
      '#type' => 'select',
      '#options' => array(
        'ol' => $this
          ->t('Ordered list'),
        'ul' => $this
          ->t('Unordered list'),
      ),
      '#title' => $this
        ->t('Categories listing style'),
      '#description' => t("This allows to select how the categories listing is presented.  It only applies to the 'Clicking on category opens the questions/answers in a new page' layout.  An ordered listing would number the categories, whereas an unordered list will have a bullet to the left of each category."),
      '#default_value' => $faq_settings
        ->get('category_listing'),
    );
    $form['faq_category_misc']['faq_category_hide_qa_accordion'] = array(
      '#type' => 'checkbox',
      '#title' => $this
        ->t('Use accordion effect for "opens/hides questions and answers under category" layout'),
      '#description' => $this
        ->t('This enables an "accordion" style effect where when a category is clicked, the questions appears beneath, and is then hidden when another category is opened.'),
      '#default_value' => $faq_settings
        ->get('category_hide_qa_accordion'),
    );
    $form['faq_category_misc']['faq_count'] = array(
      '#type' => 'checkbox',
      '#title' => $this
        ->t('Show FAQ count'),
      '#description' => $this
        ->t('This displays the number of questions in a category after the category name.'),
      '#default_value' => $faq_settings
        ->get('count'),
    );
    $form['faq_category_misc']['faq_answer_category_name'] = array(
      '#type' => 'checkbox',
      '#title' => $this
        ->t('Display category name for answers'),
      '#description' => t("This allows the user to toggle the visibility of the category name above each answer section for the 'Clicking on question takes user to answer further down the page' question/answer display."),
      '#default_value' => $faq_settings
        ->get('answer_category_name'),
    );
    $form['faq_category_misc']['faq_group_questions_top'] = array(
      '#type' => 'checkbox',
      '#title' => t("Group questions and answers for 'Categories inline'"),
      '#description' => t("This controls how categories are implemented with the 'Clicking on question takes user to answer further down the page' question/answer display."),
      '#default_value' => $faq_settings
        ->get('group_questions_top'),
    );
    $form['faq_category_misc']['faq_hide_child_terms'] = array(
      '#type' => 'checkbox',
      '#title' => $this
        ->t('Only show sub-categories when parent category is selected'),
      '#description' => t("This allows the user more control over how and when sub-categories are displayed.  It does not affect the 'Categories inline' display."),
      '#default_value' => $faq_settings
        ->get('hide_child_terms'),
    );
    $form['faq_category_misc']['faq_show_term_page_children'] = array(
      '#type' => 'checkbox',
      '#title' => $this
        ->t('Show sub-categories on FAQ category pages'),
      '#description' => t("Sub-categories with 'faq' nodes will be displayed on the per category FAQ page.  This will also happen if 'Only show sub-categories when parent category is selected' is set."),
      '#default_value' => $faq_settings
        ->get('show_term_page_children'),
    );
    $moduleHandler = \Drupal::moduleHandler();
    if ($moduleHandler
      ->moduleExists('taxonomy')) {
      $form['faq_category_advanced'] = array(
        '#type' => 'details',
        '#title' => $this
          ->t('Advanced category settings'),
        '#open' => FALSE,
      );
      $vocab_options = array();
      $vocabularies = Vocabulary::loadMultiple();
      foreach ($vocabularies as $vid => $vobj) {
        $vocab_options[$vid] = $vobj
          ->get('name');
      }
      if (!empty($vocab_options)) {
        $form['faq_category_advanced']['faq_omit_vocabulary'] = array(
          '#type' => 'checkboxes',
          '#title' => $this
            ->t('Omit vocabulary'),
          '#description' => $this
            ->t('Terms from these vocabularies will be <em>excluded</em> from the FAQ pages.'),
          '#default_value' => $faq_settings
            ->get('omit_vocabulary'),
          '#options' => $vocab_options,
          '#multiple' => TRUE,
        );
      }
    }
    return parent::buildForm($form, $form_state);
  }

  /**
   * {@inheritdoc}
   */
  public function submitForm(array &$form, FormStateInterface $form_state) {

    // Remove unnecessary values.
    $form_state
      ->cleanValues();
    $this
      ->configFactory()
      ->getEditable('faq.settings')
      ->set('use_categories', $form_state
      ->getValue('faq_use_categories'))
      ->set('category_display', $form_state
      ->getValue('faq_category_display'))
      ->set('category_listing', $form_state
      ->getValue('faq_category_listing'))
      ->set('category_hide_qa_accordion', $form_state
      ->getValue('faq_category_hide_qa_accordion'))
      ->set('count', $form_state
      ->getValue('faq_count'))
      ->set('answer_category_name', $form_state
      ->getValue('faq_answer_category_name'))
      ->set('group_questions_top', $form_state
      ->getValue('faq_group_questions_top'))
      ->set('hide_child_terms', $form_state
      ->getValue('faq_hide_child_terms'))
      ->set('show_term_page_children', $form_state
      ->getValue('faq_show_term_page_children'))
      ->set('omit_vocabulary', $form_state
      ->getValue('faq_omit_vocabulary'))
      ->save();
    parent::submitForm($form, $form_state);
  }

}

Members

Namesort descending Modifiers Type Description Overrides
CategoriesForm::buildForm public function Form constructor. Overrides ConfigFormBase::buildForm
CategoriesForm::getEditableConfigNames protected function Gets the configuration names that will be editable. Overrides ConfigFormBaseTrait::getEditableConfigNames
CategoriesForm::getFormId public function Returns a unique string identifying the form. Overrides FormInterface::getFormId
CategoriesForm::submitForm public function Form submission handler. Overrides ConfigFormBase::submitForm
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.
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.
FormBase::validateForm public function Form validation handler. Overrides FormInterface::validateForm 62
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.
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.