You are here

public function AdminSettingsForm::buildForm in Exclude Node Title 8

Form constructor.

Parameters

array $form: An associative array containing the structure of the form.

\Drupal\Core\Form\FormStateInterface $form_state: The current state of the form.

Return value

array The form structure.

Overrides ConfigFormBase::buildForm

File

src/Form/AdminSettingsForm.php, line 93

Class

AdminSettingsForm
Form object class for Exclude Node Title settings.

Namespace

Drupal\exclude_node_title\Form

Code

public function buildForm(array $form, FormStateInterface $form_state) {
  $enabled_link = Link::fromTextAndUrl(t('Search module'), Url::fromRoute('system.modules_list', [], [
    'fragment' => 'module-search',
  ]))
    ->toString();
  $form['#attached']['library'][] = 'system/drupal.system';
  $form['exclude_node_title_search'] = [
    '#type' => 'checkbox',
    '#title' => $this
      ->t('Remove node title from search pages'),
    '#description' => $this
      ->t('You need to have @searchmodule enabled.', [
      '@searchmodule' => $enabled_link,
    ]),
    '#default_value' => $this->excludeNodeTitleManager
      ->isSearchExcluded(),
    '#disabled' => !\Drupal::moduleHandler()
      ->moduleExists('search'),
  ];
  $form['render_type'] = [
    '#type' => 'radios',
    '#title' => $this
      ->t('Type of rendering'),
    '#options' => [
      'remove' => $this
        ->t('Remove text'),
      'hidden' => $this
        ->t('Hidden class'),
    ],
    '#description' => $this
      ->t('Remove text will remove all text within the title. This may leave the HTML tag. Hidden class will add a <code>.hidden</code> class to the HTML tag where appropriate.'),
    '#default_value' => $this->excludeNodeTitleManager
      ->getRenderType(),
  ];
  $form['content_type'] = [
    '#type' => 'fieldset',
    '#title' => $this
      ->t('Exclude title by content types'),
    '#description' => $this
      ->t('<strong>All nodes..</strong> excludes the Node title from all of the node displays using the View Mode(s) you select.<br /><strong>User defined nodes..</strong> does not, by default, hide any Node title. However, it provides users with the permission to exclude node title a checkbox on the node edit form that allows them to exclude node titles, from the View Modes selected in this form, on a node-by-node basis.'),
    '#collapsible' => TRUE,
    '#collapsed' => FALSE,
    '#tree' => TRUE,
  ];
  foreach ($this->bundleInfo
    ->getBundleInfo('node') as $node_type => $node_type_info) {
    $form['#attached']['drupalSettings']['exclude_node_title']['content_types'][$node_type] = $node_type_info['label'];
    $form['content_type'][$node_type]['content_type_value'] = [
      '#type' => 'select',
      '#title' => $node_type_info['label'],
      '#default_value' => $this->excludeNodeTitleManager
        ->getBundleExcludeMode($node_type),
      '#options' => [
        'none' => $this
          ->t('None'),
        'all' => $this
          ->t('All nodes...'),
        'user' => $this
          ->t('User defined nodes...'),
      ],
    ];
    $entity_view_modes = $this->entityDisplayRepository
      ->getViewModes('node');
    $modes = [];
    foreach ($entity_view_modes as $view_mode_name => $view_mode_info) {
      $modes[$view_mode_name] = $view_mode_info['label'];
    }
    $modes += [
      'nodeform' => $this
        ->t('Node form'),
    ];
    switch ($form['content_type'][$node_type]['content_type_value']['#default_value']) {
      case 'all':
        $title = $this
          ->t('Exclude title from all nodes in the following view modes:');
        break;
      case 'user defined':
        $title = $this
          ->t('Exclude title from user defined nodes in the following view modes:');
        break;
      default:
        $title = $this
          ->t('Exclude from:');
    }
    $form['content_type'][$node_type]['content_type_modes'] = [
      '#type' => 'checkboxes',
      '#title' => $title,
      '#default_value' => $this->excludeNodeTitleManager
        ->getExcludedViewModes($node_type),
      '#options' => $modes,
      '#states' => [
        // Hide the modes when the content type value is <none>.
        'invisible' => [
          'select[name="content_type[' . $node_type . '][content_type_value]"]' => [
            'value' => 'none',
          ],
        ],
      ],
    ];
  }
  $form['#attached']['library'][] = 'exclude_node_title/drupal.exclude_node_title.admin';
  return parent::buildForm($form, $form_state);
}