You are here

public function View::getForm in Entity Browser 8.2

Same name in this branch
  1. 8.2 src/Plugin/EntityBrowser/Widget/View.php \Drupal\entity_browser\Plugin\EntityBrowser\Widget\View::getForm()
  2. 8.2 src/Plugin/EntityBrowser/SelectionDisplay/View.php \Drupal\entity_browser\Plugin\EntityBrowser\SelectionDisplay\View::getForm()
Same name and namespace in other branches
  1. 8 src/Plugin/EntityBrowser/Widget/View.php \Drupal\entity_browser\Plugin\EntityBrowser\Widget\View::getForm()

Returns widget form.

Parameters

array $original_form: Entire form bult up to this point. Form elements for widget should generally not be added directly to it but returned from funciton as a separated unit.

\Drupal\Core\Form\FormStateInterface $form_state: Form state object.

array $additional_widget_parameters: Additional parameters that we want to pass to the widget.

Return value

array Form structure.

Overrides WidgetBase::getForm

File

src/Plugin/EntityBrowser/Widget/View.php, line 91

Class

View
Uses a view to provide entity listing in a browser's widget.

Namespace

Drupal\entity_browser\Plugin\EntityBrowser\Widget

Code

public function getForm(array &$original_form, FormStateInterface $form_state, array $additional_widget_parameters) {
  $form = parent::getForm($original_form, $form_state, $additional_widget_parameters);

  // TODO - do we need better error handling for view and view_display (in
  // case either of those is nonexistent or display not of correct type)?
  $form['#attached']['library'] = [
    'entity_browser/view',
  ];

  /** @var \Drupal\views\ViewExecutable $view */
  $view = $this->entityTypeManager
    ->getStorage('view')
    ->load($this->configuration['view'])
    ->getExecutable();
  if (!empty($this->configuration['arguments'])) {
    if (!empty($additional_widget_parameters['path_parts'])) {
      $arguments = [];

      // Map configuration arguments with original path parts.
      foreach ($this->configuration['arguments'] as $argument) {
        $arguments[] = isset($additional_widget_parameters['path_parts'][$argument]) ? $additional_widget_parameters['path_parts'][$argument] : '';
      }
      $view
        ->setArguments(array_values($arguments));
    }
  }
  $form['view'] = $view
    ->executeDisplay($this->configuration['view_display']);
  if (empty($view->field['entity_browser_select'])) {
    $url = Url::fromRoute('entity.view.edit_form', [
      'view' => $this->configuration['view'],
    ])
      ->toString();
    if ($this->currentUser
      ->hasPermission('administer views')) {
      return [
        '#markup' => $this
          ->t('Entity browser select form field not found on a view. <a href=":link">Go fix it</a>!', [
          ':link' => $url,
        ]),
      ];
    }
    else {
      return [
        '#markup' => $this
          ->t('Entity browser select form field not found on a view. Go fix it!'),
      ];
    }
  }

  // When rebuilding makes no sense to keep checkboxes that were previously
  // selected.
  if (!empty($form['view']['entity_browser_select'])) {
    foreach (Element::children($form['view']['entity_browser_select']) as $child) {
      $form['view']['entity_browser_select'][$child]['#process'][] = [
        '\\Drupal\\entity_browser\\Plugin\\EntityBrowser\\Widget\\View',
        'processCheckbox',
      ];
      $form['view']['entity_browser_select'][$child]['#process'][] = [
        '\\Drupal\\Core\\Render\\Element\\Checkbox',
        'processAjaxForm',
      ];
      $form['view']['entity_browser_select'][$child]['#process'][] = [
        '\\Drupal\\Core\\Render\\Element\\Checkbox',
        'processGroup',
      ];
    }
  }
  $form['view']['view'] = [
    '#markup' => \Drupal::service('renderer')
      ->render($form['view']['view']),
  ];
  return $form;
}