View source
<?php
namespace Drupal\paragraph_view_mode\Plugin\Field\FieldWidget;
use Drupal\Component\Utility\NestedArray;
use Drupal\Core\Field\FieldDefinitionInterface;
use Drupal\Core\Field\FieldItemListInterface;
use Drupal\Core\Field\Plugin\Field\FieldWidget\StringTextfieldWidget;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Plugin\ContainerFactoryPluginInterface;
use Drupal\Core\StringTranslation\TranslatableMarkup;
use Drupal\Core\Url;
use Drupal\paragraph_view_mode\StorageManagerInterface;
use Drupal\paragraph_view_mode\ViewModeInterface;
use Drupal\paragraphs\Entity\ParagraphsType;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\RequestStack;
class ParagraphViewModeWidget extends StringTextfieldWidget implements ContainerFactoryPluginInterface {
protected $request;
public function __construct($plugin_id, $plugin_definition, FieldDefinitionInterface $field_definition, array $settings, array $third_party_settings, RequestStack $request) {
parent::__construct($plugin_id, $plugin_definition, $field_definition, $settings, $third_party_settings);
$this->request = $request
->getCurrentRequest();
}
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('request_stack'));
}
public static function defaultSettings() {
return [
'view_modes' => self::getAvailableViewModes(),
'default_view_mode' => ViewModeInterface::DEFAULT,
];
}
public function settingsForm(array $form, FormStateInterface $form_state) {
$element['view_modes'] = [
'#type' => 'checkboxes',
'#title' => $this
->t('Available view modes'),
'#description' => $this
->getViewModesFieldDescription(),
'#options' => $this
->defaultSettings()['view_modes'],
'#default_value' => array_keys($this
->getEnabledViewModes()),
'#required' => FALSE,
'#ajax' => [
'callback' => [
__CLASS__,
'defaultViewModes',
],
'event' => 'change',
'wrapper' => 'paragraph-view-mode-default',
],
];
if ($this
->getSetting('view_modes')) {
$element['default_view_mode'] = [
'#type' => 'select',
'#title' => $this
->t('Default value'),
'#description' => $this
->t('View mode to be used as a default field value.'),
'#options' => $element['view_modes']['#options'],
'#default_value' => $this
->getSetting('default_view_mode'),
'#required' => FALSE,
'#weight' => 2,
'#prefix' => '<div id="paragraph-view-mode-default">',
'#suffix' => '</div>',
];
}
return $element;
}
public static function defaultViewModes(array $form, FormStateInterface $form_state) {
$checkboxes = $form_state
->getTriggeringElement();
$element = NestedArray::getValue($form, array_slice($checkboxes['#array_parents'], 0, count($checkboxes['#array_parents']) - 2));
$options = array_intersect_key($element['view_modes']['#options'], $element['view_modes']['#value']);
$element['default_view_mode']['#options'] = empty($options) ? $element['view_modes']['#options'] : $options;
return $element['default_view_mode'];
}
public function settingsSummary() {
$settings = $this
->getEnabledViewModes();
if (empty($settings)) {
$message = $this
->t('No view modes enabled, "@default" view mode will be used instead.', [
'@default' => ViewModeInterface::DEFAULT,
]);
}
else {
$message = $this
->t('Available view modes: @types', [
'@types' => implode(', ', $settings),
]);
}
$summary[] = $message;
return $summary;
}
public function formElement(FieldItemListInterface $items, $delta, array $element, array &$form, FormStateInterface $form_state) {
$element = parent::formElement($items, $delta, $element, $form, $form_state);
$element['value'] = [
'#title' => $items
->getFieldDefinition()
->getLabel(),
'#type' => 'select',
'#default_value' => isset($items[$delta]->value) ? $items[$delta]->value : $this
->getSetting('default_view_mode'),
'#options' => $this
->getEnabledViewModes() ?: $this
->getDefaultOption(),
'#required' => TRUE,
'#weight' => 1,
];
return $element;
}
protected static function getAvailableViewModes() {
$request = \Drupal::request();
$entity_display_respository = \Drupal::service('entity_display.repository');
$paragraph_type = self::getParagraphsTypeFromRequest($request);
$entity_id = StorageManagerInterface::ENTITY_TYPE;
if ($paragraph_type instanceof ParagraphsType) {
return $entity_display_respository
->getViewModeOptionsByBundle($entity_id, $paragraph_type
->id());
}
return $entity_display_respository
->getViewModeOptions($entity_id);
}
protected function getEnabledViewModes() {
$availableViewModes = self::getAvailableViewModes();
$currentViewModes = array_filter($this
->getSetting('view_modes'));
return array_intersect_key($availableViewModes, $currentViewModes);
}
protected function getDefaultOption() {
return [
ViewModeInterface::DEFAULT => $this
->t('Default'),
];
}
protected static function getParagraphsTypeFromRequest(Request $request) : ?ParagraphsType {
return $request->attributes
->get('paragraphs_type', NULL);
}
protected function getViewModesFieldDescription() : TranslatableMarkup {
$paragraphs_type = self::getParagraphsTypeFromRequest($this->request);
$url_route = implode('.', [
'entity.entity_view_display',
StorageManagerInterface::ENTITY_TYPE,
ViewModeInterface::DEFAULT,
]);
$url_paramters = [
'paragraphs_type' => $paragraphs_type
->id(),
];
$url_options = [
'fragment' => 'edit-modes',
];
$url = Url::fromRoute($url_route, $url_paramters, $url_options);
return $this
->t('It is using only the view modes enabled in the <strong>CUSTOM DISPLAY SETTINGS</strong> section under the <a href="@url">Manage Display</a> tab.', [
'@url' => $url
->toString(),
]);
}
}