You are here

class View in Entity Browser 8

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

Displays current selection in a View.

Plugin annotation


@EntityBrowserSelectionDisplay(
  id = "view",
  label = @Translation("View selection display"),
  description = @Translation("Use a pre-configured view as selection area."),
  acceptPreselection = TRUE,
  provider = "views",
  js_commands = FALSE
)

Hierarchy

Expanded class hierarchy of View

File

src/Plugin/EntityBrowser/SelectionDisplay/View.php, line 23

Namespace

Drupal\entity_browser\Plugin\EntityBrowser\SelectionDisplay
View source
class View extends SelectionDisplayBase {

  /**
   * {@inheritdoc}
   */
  public function defaultConfiguration() {
    return [
      'view' => NULL,
      'view_display' => NULL,
    ] + parent::defaultConfiguration();
  }

  /**
   * {@inheritdoc}
   */
  public function getForm(array &$original_form, FormStateInterface $form_state) {
    $form = [];

    // 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)?
    $storage =& $form_state
      ->getStorage();
    if (empty($storage['selection_display_view']) || $form_state
      ->isRebuilding()) {
      $storage['selection_display_view'] = $this->entityTypeManager
        ->getStorage('view')
        ->load($this->configuration['view'])
        ->getExecutable();
    }

    // TODO - if there are entities that are selected multiple times this
    // displays them only once. Reason for that is how SQL and Views work and
    // we probably can't do much about it.
    $selected_entities = $form_state
      ->get([
      'entity_browser',
      'selected_entities',
    ]);
    if (!empty($selected_entities)) {
      $ids = array_map(function (EntityInterface $item) {
        return $item
          ->id();
      }, $selected_entities);
      $storage['selection_display_view']
        ->setArguments([
        implode(',', $ids),
      ]);
    }
    $form['view'] = $storage['selection_display_view']
      ->executeDisplay($this->configuration['view_display']);
    $form['use_selected'] = [
      '#type' => 'submit',
      '#value' => $this
        ->t('Use selection'),
      '#name' => 'use_selected',
    ];
    return $form;
  }

  /**
   * {@inheritdoc}
   */
  public function submit(array &$form, FormStateInterface $form_state) {
    if ($form_state
      ->getTriggeringElement()['#name'] == 'use_selected') {
      $this
        ->selectionDone($form_state);
    }
  }

  /**
   * {@inheritdoc}
   */
  public function buildConfigurationForm(array $form, FormStateInterface $form_state) {
    $options = [];

    // Get all views displays.
    $views = Views::getAllViews();
    foreach ($views as $view_id => $view) {
      foreach ($view
        ->get('display') as $display_id => $display) {
        $options[$view_id . '.' . $display_id] = $this
          ->t('@view : @display', [
          '@view' => $view
            ->label(),
          '@display' => $display['display_title'],
        ]);
      }
    }
    $form['view'] = [
      '#type' => 'select',
      '#title' => $this
        ->t('View : View display'),
      '#default_value' => $this->configuration['view'] . '.' . $this->configuration['view_display'],
      '#options' => $options,
      '#required' => TRUE,
      '#description' => $this
        ->t('View display to use for displaying currently selected items. Do note that to get something useful out of this display, its first contextual filter should be a filter on the primary identifier field of your entity type (e.g., Node ID, Media ID).'),
    ];
    return $form;
  }

  /**
   * {@inheritdoc}
   */
  public function submitConfigurationForm(array &$form, FormStateInterface $form_state) {
    $values = $form_state
      ->getValues();
    if (!empty($values['view'])) {
      list($view_id, $display_id) = explode('.', $values['view']);
      $this->configuration['view'] = $view_id;
      $this->configuration['view_display'] = $display_id;
    }
  }

  /**
   * {@inheritdoc}
   */
  public function calculateDependencies() {
    $dependencies = [];
    if ($this->configuration['view']) {
      $view = ViewEntity::load($this->configuration['view']);
      $dependencies[$view
        ->getConfigDependencyKey()] = [
        $view
          ->getConfigDependencyName(),
      ];
    }
    return $dependencies;
  }

}

Members

Namesort descending Modifiers Type Description Overrides
DependencySerializationTrait::$_entityStorages protected property An array of entity type IDs keyed by the property name of their storages.
DependencySerializationTrait::$_serviceIds protected property An array of service IDs keyed by property name used for serialization.
DependencySerializationTrait::__sleep public function 1
DependencySerializationTrait::__wakeup public function 2
MessengerTrait::$messenger protected property The messenger. 29
MessengerTrait::messenger public function Gets the messenger. 29
MessengerTrait::setMessenger public function Sets the messenger.
PluginBase::$configuration protected property Configuration information passed into the plugin. 1
PluginBase::$pluginDefinition protected property The plugin implementation definition. 1
PluginBase::$pluginId protected property The plugin_id.
PluginBase::DERIVATIVE_SEPARATOR constant A string which is used to separate base plugin IDs from the derivative ID.
PluginBase::getBaseId public function Gets the base_plugin_id of the plugin instance. Overrides DerivativeInspectionInterface::getBaseId
PluginBase::getDerivativeId public function Gets the derivative_id of the plugin instance. Overrides DerivativeInspectionInterface::getDerivativeId
PluginBase::getPluginDefinition public function Gets the definition of the plugin implementation. Overrides PluginInspectionInterface::getPluginDefinition 3
PluginBase::getPluginId public function Gets the plugin_id of the plugin instance. Overrides PluginInspectionInterface::getPluginId
PluginBase::isConfigurable public function Determines if the plugin is configurable.
PluginConfigurationFormTrait::validateConfigurationForm public function Implements PluginFormInterface::validateConfigurationForm(). 2
SelectionDisplayBase::$entityTypeManager protected property Entity manager service.
SelectionDisplayBase::$eventDispatcher protected property Event dispatcher service.
SelectionDisplayBase::$label protected property Plugin label.
SelectionDisplayBase::checkPreselectionSupport public function Check does selection display support preselection. Overrides SelectionDisplayInterface::checkPreselectionSupport
SelectionDisplayBase::create public static function Creates an instance of the plugin. Overrides ContainerFactoryPluginInterface::create 1
SelectionDisplayBase::getConfiguration public function Gets this plugin's configuration. Overrides ConfigurableInterface::getConfiguration
SelectionDisplayBase::label public function Returns the selection display label. Overrides SelectionDisplayInterface::label
SelectionDisplayBase::selectionDone protected function Marks selection as done - sets value in form state and dispatches event.
SelectionDisplayBase::setConfiguration public function Sets the configuration for this plugin instance. Overrides ConfigurableInterface::setConfiguration
SelectionDisplayBase::supportsJsCommands public function Returns true if selection display supports selection over javascript. Overrides SelectionDisplayInterface::supportsJsCommands
SelectionDisplayBase::supportsPreselection public function Check if the plugin supports preselection. Overrides SelectionDisplayInterface::supportsPreselection
SelectionDisplayBase::validate public function Validates form. Overrides SelectionDisplayInterface::validate
SelectionDisplayBase::__construct public function Constructs widget plugin. Overrides PluginBase::__construct 1
StringTranslationTrait::$stringTranslation protected property The string translation service. 1
StringTranslationTrait::formatPlural protected function Formats a string containing a count of items.
StringTranslationTrait::getNumberOfPlurals protected function Returns the number of plurals supported by a given language.
StringTranslationTrait::getStringTranslation protected function Gets the string translation service.
StringTranslationTrait::setStringTranslation public function Sets the string translation service to use. 2
StringTranslationTrait::t protected function Translates a string to the current language or to a given language.
View::buildConfigurationForm public function Implements PluginFormInterface::buildConfigurationForm(). Overrides PluginConfigurationFormTrait::buildConfigurationForm
View::calculateDependencies public function Calculates dependencies for the configured plugin. Overrides SelectionDisplayBase::calculateDependencies
View::defaultConfiguration public function Gets default configuration for this plugin. Overrides SelectionDisplayBase::defaultConfiguration
View::getForm public function Returns selection display form. Overrides SelectionDisplayInterface::getForm
View::submit public function Submits form. Overrides SelectionDisplayBase::submit
View::submitConfigurationForm public function Implements PluginFormInterface::submitConfigurationForm(). Overrides PluginConfigurationFormTrait::submitConfigurationForm