You are here

public function View::buildOptionsForm in Views Field View 8

Default options form that provides the label widget that all fields should have.

Overrides FieldPluginBase::buildOptionsForm

File

src/Plugin/views/field/View.php, line 92

Class

View
Plugin annotation @ViewsField("view");

Namespace

Drupal\views_field_view\Plugin\views\field

Code

public function buildOptionsForm(&$form, FormStateInterface $form_state) {
  parent::buildOptionsForm($form, $form_state);
  $view_options = Views::getViewsAsOptions(TRUE, 'all', NULL, FALSE, TRUE);
  $form['views_field_view'] = [
    '#type' => 'details',
    '#title' => $this
      ->t("View settings"),
    '#open' => TRUE,
  ];
  $form['view'] = [
    '#type' => 'select',
    '#title' => $this
      ->t('View'),
    '#description' => $this
      ->t('Select a view to embed.'),
    '#default_value' => $this->options['view'],
    '#options' => $view_options,
    '#ajax' => [
      'path' => views_ui_build_form_url($form_state),
    ],
    '#submit' => [
      [
        $this,
        'submitTemporaryForm',
      ],
    ],
    '#executes_submit_callback' => TRUE,
    '#fieldset' => 'views_field_view',
  ];

  // If there is no view set, use the first one for now.
  if (count($view_options) && empty($this->options['view'])) {
    $new_options = array_keys($view_options);
    $this->options['view'] = reset($new_options);
  }
  if ($this->options['view']) {
    $view = Views::getView($this->options['view']);
    $display_options = [];
    foreach ($view->storage
      ->get('display') as $name => $display) {

      // Allow to embed a different display as the current one.
      if ($this->options['view'] != $this->view->storage
        ->id() || $this->view->current_display != $name) {
        $display_options[$name] = $display['display_title'];
      }
    }
    $form['display'] = [
      '#type' => 'select',
      '#title' => $this
        ->t('Display'),
      '#description' => $this
        ->t('Select a view display to use.'),
      '#default_value' => $this->options['display'],
      '#options' => $display_options,
      '#ajax' => [
        'path' => views_ui_build_form_url($form_state),
      ],
      '#submit' => [
        [
          $this,
          'submitTemporaryForm',
        ],
      ],
      '#executes_submit_callback' => TRUE,
      '#fieldset' => 'views_field_view',
    ];

    // Provide a way to directly access the views edit link of the child view.
    // Don't show this link if the current view is the selected child view.
    if (!empty($this->options['view']) && !empty($this->options['display']) && $this->view->storage
      ->id() != $this->options['view']) {

      // use t() here, and set HTML on #link options.
      $link_text = $this
        ->t('Edit "%view (@display)" view', [
        '%view' => $view_options[$this->options['view']],
        '@display' => $this->options['display'],
      ]);
      $form['view_edit'] = [
        '#type' => 'container',
        '#fieldset' => 'views_field_view',
      ];
      $form['view_edit']['view_edit_link'] = [
        '#type' => 'link',
        '#title' => $link_text,
        '#url' => Url::fromRoute('entity.view.edit_display_form', [
          'view' => $this->options['view'],
          'display_id' => $this->options['display'],
        ], [
          'attributes' => [
            'target' => '_blank',
            'class' => [
              'views-field-view-child-view-edit',
            ],
          ],
          'html' => TRUE,
        ]),
        '#attached' => [
          'library' => [
            'views_field_view/drupal.views_field_view',
          ],
        ],
        '#prefix' => '<span>[</span>',
        '#suffix' => '<span>]</span>',
      ];
      $form['view_edit']['description'] = [
        '#markup' => $this
          ->t('Use this link to open the current child view\'s edit page in a new window.'),
        '#prefix' => '<div class="description">',
        '#suffix' => '</div>',
      ];
    }
    $form['arguments'] = [
      '#title' => $this
        ->t('Contextual filters'),
      '#description' => $this
        ->t('Use a comma (,) or forwardslash (/) separated list of each contextual filter which should be forwarded to the view.
          See below list of available replacement tokens. Static values are also be passed to child views if they do not match a token format.
          You could pass static ID\'s or taxonomy terms in this way. E.g. 123 or "my taxonomy term".'),
      '#type' => 'textfield',
      '#default_value' => $this->options['arguments'],
      '#fieldset' => 'views_field_view',
    ];
    $form['available_tokens'] = [
      '#type' => 'details',
      '#title' => $this
        ->t('Replacement patterns'),
      '#value' => $this
        ->getTokenInfo(),
      '#fieldset' => 'views_field_view',
    ];
  }
  $form['alter']['#access'] = FALSE;
}