You are here

public function MenuAttributesSettingsForm::buildForm in Menu Attributes 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/MenuAttributesSettingsForm.php, line 32

Class

MenuAttributesSettingsForm
Configure Menu Attributes admin settings.

Namespace

Drupal\menu_attributes\Form

Code

public function buildForm(array $form, FormStateInterface $form_state) {
  if (!\Drupal::currentUser()
    ->hasPermission('administer menu attributes')) {
    return;
  }
  $config = $this
    ->config('menu_attributes.settings');
  $form['attributes_title'] = [
    '#type' => 'item',
    '#title' => t('Menu item attribute options'),
  ];
  $form['attributes_vertical_tabs'] = [
    '#type' => 'vertical_tabs',
    '#attached' => [
      'library' => [
        'menu_attributes/option_summary',
      ],
    ],
  ];
  $form['#tree'] = TRUE;
  $attributes = menu_attributes_get_menu_attribute_info();
  foreach ($attributes as $attribute => $info) {
    $form['attributes'][$attribute] = [
      '#type' => 'details',
      '#title' => $info['label'],
      '#group' => 'attributes_vertical_tabs',
      '#description' => $info['form']['#description'],
    ];
    $form['attributes'][$attribute]['attribute_enable'] = [
      '#type' => 'checkbox',
      '#title' => t('Enable the @attribute attribute.', [
        '@attribute' => mb_strtolower($info['label']),
      ]),
      '#default_value' => $info['enabled'],
    ];
    $form['attributes'][$attribute]['attribute_default'] = [
      '#title' => t('Default'),
      '#description' => '',
      '#states' => [
        'invisible' => [
          'input[name="attributes[' . $attribute . '][attribute_enable]"]' => [
            'checked' => FALSE,
          ],
        ],
      ],
    ] + $info['form'];
  }
  return parent::buildForm($form, $form_state);
}