You are here

public function MenuPositionRuleForm::form in Menu Position 8

Gets the actual form array to be built.

Overrides EntityForm::form

See also

\Drupal\Core\Entity\EntityForm::processForm()

\Drupal\Core\Entity\EntityForm::afterBuild()

File

src/Form/MenuPositionRuleForm.php, line 119

Class

MenuPositionRuleForm
The Menu Position rule form.

Namespace

Drupal\menu_position\Form

Code

public function form(array $form, FormStateInterface $form_state) {

  // Allow parent to construct base form, set tree value.
  $form = parent::form($form, $form_state);
  $form['#tree'] = TRUE;

  // Set these for use when attaching condition forms.
  $form_state
    ->setTemporaryValue('gathered_contexts', $this->context_repository
    ->getAvailableContexts());

  // Get the menu position rule entity.

  /** @var \Drupal\menu_position\Entity\MenuPositionRule $rule */
  $rule = $this->entity;

  // Get the menu link for this rule.
  $menu_link = $rule
    ->getMenuLinkPlugin();

  // Menu position label.
  $form['label'] = [
    '#type' => 'textfield',
    '#title' => $this
      ->t('Label'),
    '#maxlength' => 255,
    '#default_value' => $rule
      ->getLabel(),
    '#description' => $this
      ->t("Label for the Menu Position rule."),
    '#required' => TRUE,
  ];

  // Menu position machine name.
  $form['id'] = [
    '#type' => 'machine_name',
    '#default_value' => $rule
      ->getId(),
    '#machine_name' => [
      'exists' => [
        $this,
        'exist',
      ],
    ],
    '#disabled' => !$rule
      ->isNew(),
  ];
  $form['enabled'] = [
    '#type' => 'checkbox',
    '#title' => $this
      ->t('Enabled'),
    '#description' => $this
      ->t('A flag for whether the menu position rule should be evaluated or is ignored.'),
    '#default_value' => $rule
      ->isNew() ? TRUE : $rule
      ->getEnabled(),
  ];

  // Menu position parent menu tree item.
  $options = $this->menu_parent_form_selector
    ->getParentSelectOptions();
  $form['parent'] = [
    '#type' => 'select',
    '#title' => $this
      ->t('Parent menu item'),
    '#required' => TRUE,
    '#default_value' => !$rule
      ->isNew() ? $menu_link
      ->getMenuName() . ':' . $menu_link
      ->getParent() : NULL,
    '#options' => $options,
    '#description' => $this
      ->t('Select the place in the menu where the rule should position its menu links.'),
    '#attributes' => [
      'class' => [
        'menu-parent-select',
      ],
    ],
  ];

  // Menu position conditions vertical tabs.
  $form['conditions'] = [
    'conditions_tabs' => [
      '#type' => 'vertical_tabs',
      '#title' => $this
        ->t('Conditions'),
      '#description' => $this
        ->t('All the conditions must be met before a rule is applied.'),
      '#parents' => [
        'conditions_tabs',
      ],
    ],
  ];

  // Get all available plugins from the plugin manager.
  foreach ($this->condition_plugin_manager
    ->getDefinitionsForContexts($form_state
    ->getTemporaryValue('gathered_contexts')) as $condition_id => $definition) {

    // If this condition exists already on the rule, use that.
    if ($rule
      ->getConditions()
      ->has($condition_id)) {
      $condition = $rule
        ->getConditions()
        ->get($condition_id);
    }
    else {
      $condition = $this->condition_plugin_manager
        ->createInstance($condition_id, []);
    }

    // Set conditions in the form state for extraction later.
    $form_state
      ->set([
      'conditions',
      $condition_id,
    ], $condition);

    // Allow condition plugins to build their own forms.
    $condition_form = $condition
      ->buildConfigurationForm([], $form_state);
    $condition_form['#type'] = 'details';
    $condition_form['#title'] = $condition
      ->getPluginDefinition()['label'];
    $condition_form['#group'] = 'conditions_tabs';
    $form['conditions'][$condition_id] = $condition_form;
  }

  // Custom form alters for core conditions (lifted from BlockForm.php).
  if (isset($form['conditions']['node_type'])) {
    $form['conditions']['node_type']['#title'] = $this
      ->t('Content types');
    $form['conditions']['node_type']['bundles']['#title'] = $this
      ->t('Content types');
    $form['conditions']['node_type']['negate']['#type'] = 'value';
    $form['conditions']['node_type']['negate']['#title_display'] = 'invisible';
    $form['conditions']['node_type']['negate']['#value'] = $form['conditions']['node_type']['negate']['#default_value'];
  }
  if (isset($form['conditions']['user_role'])) {
    $form['conditions']['user_role']['#title'] = $this
      ->t('Roles');
    unset($form['conditions']['user_role']['roles']['#description']);
    $form['conditions']['user_role']['negate']['#type'] = 'value';
    $form['conditions']['user_role']['negate']['#value'] = $form['conditions']['user_role']['negate']['#default_value'];
  }
  if (isset($form['conditions']['current_theme'])) {
    $form['conditions']['current_theme']['theme']['#empty_value'] = '';
    $form['conditions']['current_theme']['theme']['#empty_option'] = $this
      ->t('- Any -');
  }
  if (isset($form['conditions']['request_path'])) {
    $form['conditions']['request_path']['#title'] = $this
      ->t('Pages');
    $form['conditions']['request_path']['negate']['#type'] = 'radios';
    $form['conditions']['request_path']['negate']['#default_value'] = (int) $form['conditions']['request_path']['negate']['#default_value'];
    $form['conditions']['request_path']['negate']['#title_display'] = 'invisible';
    $form['conditions']['request_path']['negate']['#options'] = [
      $this
        ->t('Show for the listed pages'),
      $this
        ->t('Hide for the listed pages'),
    ];
  }
  if (isset($form['conditions']['language'])) {
    $form['conditions']['language']['negate']['#type'] = 'value';
    $form['conditions']['language']['negate']['#value'] = $form['conditions']['language']['negate']['#default_value'];
  }
  return $form;
}