ViewModeSelectorSelect.php in Opigno certificate 8
File
src/Plugin/Field/FieldWidget/ViewModeSelectorSelect.php
View source
<?php
namespace Drupal\opigno_certificate\Plugin\Field\FieldWidget;
use Drupal\Core\Entity\EntityDisplayRepository;
use Drupal\Core\Field\FieldDefinitionInterface;
use Drupal\Core\Field\FieldItemListInterface;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Plugin\ContainerFactoryPluginInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
class ViewModeSelectorSelect extends ViewModeSelectorWidgetBase implements ContainerFactoryPluginInterface {
public function __construct($plugin_id, $plugin_definition, FieldDefinitionInterface $field_definition, array $settings, array $third_party_settings, EntityDisplayRepository $entity_display_repository) {
parent::__construct($plugin_id, $plugin_definition, $field_definition, $settings, $third_party_settings, $entity_display_repository);
$entity_type = $field_definition
->getTargetEntityTypeId();
$view_modes = $entity_display_repository
->getViewModeOptions($entity_type);
unset($view_modes['view_mode_selector']);
$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'));
}
public function formElement(FieldItemListInterface $items, $delta, array $element, array &$form, FormStateInterface $form_state) {
$element['value'] = $element + [
'#type' => 'select',
'#options' => $this->viewModes,
'#default_value' => $items[$delta]->value ?: current(array_keys($this->viewModes)),
];
return $element;
}
}