You are here

public function ViewModeSelectorWidgetBase::__construct in View Mode Selector 8

ViewModeSelectorWidgetBase constructor.

Parameters

string $plugin_id:

mixed $plugin_definition:

\Drupal\Core\Field\FieldDefinitionInterface $field_definition:

array $settings:

array $third_party_settings:

\Drupal\Core\Entity\EntityDisplayRepositoryInterface $entity_display_repository:

Overrides WidgetBase::__construct

1 call to ViewModeSelectorWidgetBase::__construct()
ViewModeSelectorIcons::__construct in src/Plugin/Field/FieldWidget/ViewModeSelectorIcons.php
ViewModeSelectorIcons constructor.
1 method overrides ViewModeSelectorWidgetBase::__construct()
ViewModeSelectorIcons::__construct in src/Plugin/Field/FieldWidget/ViewModeSelectorIcons.php
ViewModeSelectorIcons constructor.

File

src/Plugin/Field/FieldWidget/ViewModeSelectorWidgetBase.php, line 38

Class

ViewModeSelectorWidgetBase
Base class for the 'view_mode_selector_*' widgets.

Namespace

Drupal\view_mode_selector\Plugin\Field\FieldWidget

Code

public function __construct($plugin_id, $plugin_definition, FieldDefinitionInterface $field_definition, array $settings, array $third_party_settings, EntityDisplayRepositoryInterface $entity_display_repository) {
  parent::__construct($plugin_id, $plugin_definition, $field_definition, $settings, $third_party_settings);
  $this->entityDisplayRepository = $entity_display_repository;
  $field_settings = $field_definition
    ->getSettings();
  $entity_type = $field_definition
    ->getTargetEntityTypeId();
  $bundle = $field_definition
    ->getTargetBundle();

  // Get all view modes for the current bundle.
  $view_modes = $this->entityDisplayRepository
    ->getViewModeOptionsByBundle($entity_type, $bundle);

  // Reduce options by enabled view modes
  foreach (array_keys($view_modes) as $view_mode) {
    if (isset($field_settings['view_modes'][$view_mode]['enable']) && $field_settings['view_modes'][$view_mode]['enable']) {
      continue;
    }
    unset($view_modes[$view_mode]);
  }

  // Show all view modes in widget when no view modes are enabled.
  if (!count($view_modes)) {
    $view_modes = $this->entityDisplayRepository
      ->getViewModeOptionsByBundle($entity_type, $bundle);
  }
  $this->viewModes = $view_modes;
}