You are here

public function FieldMenuBlock::blockForm in Menu item content fields 8

Overrides SystemMenuBlock::blockForm

File

src/Plugin/Block/FieldMenuBlock.php, line 35

Class

FieldMenuBlock
Provides a drupal menu that uses display view modes.

Namespace

Drupal\menu_item_fields\Plugin\Block

Code

public function blockForm($form, FormStateInterface $form_state) {
  $viewModes = \Drupal::entityTypeManager()
    ->getStorage('entity_view_mode')
    ->loadByProperties([
    'targetEntityType' => 'menu_link_content',
  ]);
  $viewModeOptions = [
    'default' => $this
      ->t('Default'),
  ];
  foreach ($viewModes as $viewMode) {
    $id = substr($viewMode
      ->id(), strlen('menu_link_content.'));
    $viewModeOptions[$id] = $viewMode
      ->label();
  }
  $form['view_mode'] = [
    '#type' => 'select',
    '#title' => $this
      ->t('View mode to use when rendering the menu items'),
    '#default_value' => $this->configuration['view_mode'],
    '#description' => $this
      ->t('View mode to use when rendering menu items. <a href="@url">Configure view modes</a>', [
      '@url' => Url::fromRoute('entity.entity_view_display.menu_link_content.default')
        ->toString(),
    ]),
    '#options' => $viewModeOptions,
  ];
  $fields = \Drupal::entityTypeManager()
    ->getStorage('field_config')
    ->loadByProperties([
    'entity_type' => 'menu_link_content',
  ]);
  $fieldOptions = [
    '_none' => $this
      ->t('None'),
  ];
  foreach ($fields as $field) {
    list(, , $id) = explode('.', $field
      ->id());
    $fieldOptions[$id] = $field
      ->label();
  }
  $form['view_mode_override_field'] = [
    '#type' => 'select',
    '#title' => $this
      ->t('Field that stores the view mode override per menu item'),
    '#default_value' => $this->configuration['view_mode_override_field'],
    '#description' => $this
      ->t('This field will usually be a an options field with the available view mode ids.'),
    '#options' => $fieldOptions,
  ];
  $form += parent::blockForm($form, $form_state);
  return $form;
}