You are here

public function MenuLinkItem::fieldSettingsForm in Menu Link (Field) 8

Same name and namespace in other branches
  1. 2.0.x src/Plugin/Field/FieldType/MenuLinkItem.php \Drupal\menu_link\Plugin\Field\FieldType\MenuLinkItem::fieldSettingsForm()

Returns a form for the field-level settings.

Invoked from \Drupal\field_ui\Form\FieldConfigEditForm to allow administrators to configure field-level settings.

Parameters

array $form: The form where the settings form is being included in.

\Drupal\Core\Form\FormStateInterface $form_state: The form state of the (entire) configuration form.

Return value

array The form definition for the field settings.

Overrides FieldItemBase::fieldSettingsForm

File

src/Plugin/Field/FieldType/MenuLinkItem.php, line 109

Class

MenuLinkItem
Defines a menu link field type which stores the link, parent and menu.

Namespace

Drupal\menu_link\Plugin\Field\FieldType

Code

public function fieldSettingsForm(array $form, FormStateInterface $form_state) {
  $form = parent::fieldSettingsForm($form, $form_state);
  $menu_options = $this
    ->getMenuNames();
  $form['available_menus'] = [
    '#type' => 'checkboxes',
    '#title' => $this
      ->t('Available menus'),
    '#default_value' => $this
      ->getSetting('available_menus'),
    '#options' => $menu_options,
    '#description' => $this
      ->t('The menus available to place links in for this kind of entity.'),
    '#required' => TRUE,
  ];
  $parent_options = [];

  // Make sure the setting is normalized to an associative array.
  $available_menus = array_filter($this
    ->getSetting('available_menus'));
  $available_menus = array_combine($available_menus, $available_menus);
  foreach ($available_menus as $name) {
    if (isset($menu_options[$name])) {
      $parent_options["{$name}:"] = $menu_options[$name];
    }
  }
  $form['default_menu_parent'] = [
    '#type' => 'select',
    '#title' => $this
      ->t('Default menu for new links'),
    '#default_value' => $this
      ->getSetting('default_menu_parent'),
    '#options' => $parent_options,
  ];
  return $form;
}