ViewModeSelectorWidgetBase.php in Opigno certificate 3.x
File
src/Plugin/Field/FieldWidget/ViewModeSelectorWidgetBase.php
View source
<?php
namespace Drupal\opigno_certificate\Plugin\Field\FieldWidget;
use Drupal\Core\Entity\EntityDisplayRepositoryInterface;
use Drupal\Core\Field\WidgetBase;
use Drupal\Core\Field\FieldDefinitionInterface;
use Drupal\Core\Plugin\ContainerFactoryPluginInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
abstract class ViewModeSelectorWidgetBase extends WidgetBase implements ContainerFactoryPluginInterface {
protected $viewModes = [];
protected $entityDisplayRepository;
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();
$view_modes = $this->entityDisplayRepository
->getViewModeOptionsByBundle($entity_type, $bundle);
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]);
}
if (!count($view_modes)) {
$view_modes = $this->entityDisplayRepository
->getViewModeOptionsByBundle($entity_type, $bundle);
}
$this->viewModes = $view_modes;
}
public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
return new static($plugin_id, $plugin_definition, $configuration['field_definition'], $configuration['settings'], $configuration['third_party_settings'], $container
->get('entity_display.repository'));
}
}