You are here

public function Eva::buildOptionsForm in EVA: Entity Views Attachment 8.2

Same name and namespace in other branches
  1. 8 src/Plugin/views/display/Eva.php \Drupal\eva\Plugin\views\display\Eva::buildOptionsForm()

Provide a form to edit options for this plugin.

Overrides DisplayPluginBase::buildOptionsForm

File

src/Plugin/views/display/Eva.php, line 165

Class

Eva
The plugin that handles an EVA display in views.

Namespace

Drupal\eva\Plugin\views\display

Code

public function buildOptionsForm(&$form, FormStateInterface $form_state) {
  parent::buildOptionsForm($form, $form_state);
  $entity_info = $this->entityTypeManager
    ->getDefinitions();
  $entity_type = $this
    ->getOption('entity_type');
  switch ($form_state
    ->get('section')) {
    case 'entity_type':
      foreach ($entity_info as $type => $info) {

        // Is this a content/front-facing entity?
        if ($info instanceof ContentEntityType) {
          $entity_names[$type] = $info
            ->get('label');
        }
      }
      $form['#title'] .= $this
        ->t('Entity type');
      $form['entity_type'] = [
        '#type' => 'radios',
        '#required' => TRUE,
        '#validated' => TRUE,
        '#title' => $this
          ->t('Attach this display to the following entity type'),
        '#options' => $entity_names,
        '#default_value' => $this
          ->getOption('entity_type'),
      ];
      break;
    case 'bundles':
      $options = [];
      foreach ($this->bundleInfo
        ->getBundleInfo($entity_type) as $bundle => $info) {
        $options[$bundle] = $info['label'];
      }
      $form['#title'] .= $this
        ->t('Bundles');
      $form['bundles'] = [
        '#type' => 'checkboxes',
        '#title' => $this
          ->t('Attach this display to the following bundles.  If no bundles are selected, the display will be attached to all.'),
        '#options' => $options,
        '#default_value' => $this
          ->getOption('bundles'),
      ];
      break;
    case 'arguments':
      $form['#title'] .= $this
        ->t('Arguments');
      $default = $this
        ->getOption('argument_mode');
      $options = [
        'None' => $this
          ->t("No special handling"),
        'id' => $this
          ->t("Use the ID of the entity the view is attached to"),
        'token' => $this
          ->t("Use tokens from the entity the view is attached to"),
      ];
      $form['argument_mode'] = [
        '#type' => 'radios',
        '#title' => $this
          ->t("How should this display populate the view's arguments?"),
        '#options' => $options,
        '#default_value' => $default,
      ];
      $form['token'] = [
        '#type' => 'fieldset',
        '#title' => $this
          ->t('Token replacement'),
        '#collapsible' => TRUE,
        '#states' => [
          'visible' => [
            ':input[name=argument_mode]' => [
              'value' => 'token',
            ],
          ],
        ],
      ];
      $form['token']['default_argument'] = [
        '#title' => $this
          ->t('Arguments'),
        '#type' => 'textfield',
        '#default_value' => $this
          ->getOption('default_argument'),
        '#description' => $this
          ->t('You may use token replacement to provide arguments based on the current entity. Separate arguments with "/".'),
      ];

      // Add a token browser.
      if (\Drupal::service('module_handler')
        ->moduleExists('token')) {
        $token_types = [
          $entity_type => $entity_type,
        ];
        $token_mapper = \Drupal::service('token.entity_mapper');
        if (!empty($token_types)) {
          $token_types = array_map(function ($type) use ($token_mapper) {
            return $token_mapper
              ->getTokenTypeForEntityType($type);
          }, (array) $token_types);
        }
        $form['token']['browser'] = [
          '#theme' => 'token_tree_link',
          '#token_types' => $token_types,
          '#global_types' => TRUE,
          '#show_nested' => FALSE,
        ];
      }
      break;
    case 'show_title':
      $form['#title'] .= $this
        ->t('Show title');
      $form['show_title'] = [
        '#type' => 'checkbox',
        '#title' => $this
          ->t('Show the title of the view above the attached view.'),
        '#default_value' => $this
          ->getOption('show_title'),
      ];
      break;
  }
}