class ParagraphsViewmodeBehavior in Paragraphs View Modes 8
Class ParagraphsViewmodeBehavior.
Plugin annotation
@ParagraphsBehavior(
id="paragraphs_viewmode_behavior",
label=@Translation("Paragraphs View Mode"),
description=@Translation("A Plugin to allow overriding a paragraph view mode while on default")
)
Hierarchy
- class \Drupal\Component\Plugin\PluginBase implements DerivativeInspectionInterface, PluginInspectionInterface
- class \Drupal\Core\Plugin\PluginBase uses DependencySerializationTrait, MessengerTrait, StringTranslationTrait
- class \Drupal\paragraphs\ParagraphsBehaviorBase implements ContainerFactoryPluginInterface, ParagraphsBehaviorInterface
- class \Drupal\paragraphs_viewmode\Plugin\paragraphs\Behavior\ParagraphsViewmodeBehavior implements ParagraphsViewmodeBehaviorInterface
- class \Drupal\paragraphs\ParagraphsBehaviorBase implements ContainerFactoryPluginInterface, ParagraphsBehaviorInterface
- class \Drupal\Core\Plugin\PluginBase uses DependencySerializationTrait, MessengerTrait, StringTranslationTrait
Expanded class hierarchy of ParagraphsViewmodeBehavior
File
- src/
Plugin/ paragraphs/ Behavior/ ParagraphsViewmodeBehavior.php, line 24
Namespace
Drupal\paragraphs_viewmode\Plugin\paragraphs\BehaviorView source
class ParagraphsViewmodeBehavior extends ParagraphsBehaviorBase implements ParagraphsViewmodeBehaviorInterface {
/**
* The entity Display Repository.
*
* @var \Drupal\Core\Entity\EntityDisplayRepositoryInterface
*/
protected $entityDisplayRepository;
/**
* The entity type manager.
*
* @var \Drupal\Core\Entity\EntityTypeManagerInterface
*/
protected $entityTypeManager;
/**
* Constructs a ParagraphsViewmodeBehavior object.
*
* @param array $configuration
* A configuration array containing information about the plugin instance.
* @param string $plugin_id
* The plugin_id for the plugin instance.
* @param mixed $plugin_definition
* The plugin implementation definition.
* @param \Drupal\Core\Entity\EntityFieldManager $entity_field_manager
* The entity field manager.
* @param \Drupal\Core\Entity\EntityDisplayRepositoryInterface $entity_display_repository
* The entity display repository.
*/
public function __construct(array $configuration, $plugin_id, $plugin_definition, EntityFieldManager $entity_field_manager, EntityDisplayRepositoryInterface $entity_display_repository) {
parent::__construct($configuration, $plugin_id, $plugin_definition, $entity_field_manager);
$this->entityDisplayRepository = $entity_display_repository;
}
/**
* {@inheritdoc}
*/
public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
return new static($configuration, $plugin_id, $plugin_definition, $container
->get('entity_field.manager'), $container
->get('entity_display.repository'));
}
/**
* {@inheritdoc}
*/
public function view(array &$build, Paragraph $paragraph, EntityViewDisplayInterface $display, $view_mode) {
return $build;
}
/**
* {@inheritdoc}
*/
public function buildConfigurationForm(array $form, FormStateInterface $form_state) {
$view_modes = $this->entityDisplayRepository
->getViewModeOptions('paragraph');
$form['override_mode'] = [
'#type' => 'select',
'#title' => $this
->t('Select which view mode to override'),
'#options' => $view_modes,
'#default_value' => $this->configuration['override_mode'],
];
$form['override_available'] = [
'#type' => 'checkboxes',
'#title' => $this
->t('Select which view modes are allowable'),
'#multiple' => TRUE,
'#options' => $view_modes,
'#default_value' => $this->configuration['override_available'],
];
$form['override_default'] = [
'#type' => 'select',
'#title' => $this
->t('Select default view mode for content to use'),
'#options' => $view_modes,
'#default_value' => $this->configuration['override_default'],
];
return parent::buildConfigurationForm($form, $form_state);
}
/**
* {@inheritdoc}
*/
public function validateConfigurationForm(array &$form, FormStateInterface $form_state) {
parent::validateConfigurationForm($form, $form_state);
$default = $form_state
->getValue('override_default');
$allowed = array_filter($form_state
->getValue('override_available'));
if (!in_array($default, $allowed)) {
$form_state
->setError($form['override_default'], 'Default view mode must also be selected in allowed view modes');
}
}
/**
* {@inheritdoc}
*/
public function submitConfigurationForm(array &$form, FormStateInterface $form_state) {
$override_mode = $form_state
->getValue('override_mode');
$override_available = $form_state
->getValue('override_available');
$override_default = $form_state
->getValue('override_default');
$this->configuration['override_mode'] = $override_mode;
$this->configuration['override_available'] = array_filter($override_available);
$this->configuration['override_default'] = $override_default;
parent::submitConfigurationForm($form, $form_state);
}
/**
* {@inheritdoc}
*/
public function defaultConfiguration() {
return parent::defaultConfiguration() + [
'override_mode' => 'default',
'override_available' => [
'default' => 'Default',
],
'override_default' => 'default',
];
}
/**
* {@inheritdoc}
*/
public function buildBehaviorForm(ParagraphInterface $paragraph, array &$form, FormStateInterface $form_state) {
$all_modes = $this->entityDisplayRepository
->getViewModeOptions('paragraph');
$available_modes = array_filter($this->configuration['override_available']);
$mode = $paragraph
->getBehaviorSetting($this->pluginId, 'view_mode', $this->configuration['override_default']);
$mode_options = array_intersect_key($all_modes, $available_modes);
$form['view_mode'] = [
'#type' => 'select',
'#title' => 'Select which view mode to use for this paragraph',
'#options' => $mode_options,
'#default_value' => $mode,
];
return parent::buildBehaviorForm($paragraph, $form, $form_state);
}
/**
* {@inheritdoc}
*/
public function submitBehaviorForm(ParagraphInterface $paragraph, array &$form, FormStateInterface $form_state) {
$paragraph
->setBehaviorSettings($this->pluginId, [
'view_mode',
$form_state
->getValue('view_mode'),
]);
parent::submitBehaviorForm($paragraph, $form, $form_state);
}
/**
* {@inheritdoc}
*/
public function entityViewModeAlter(&$view_mode, ParagraphInterface $paragraph, array $context) {
$override_mode = $this->configuration['override_mode'];
$new_view_mode = $paragraph
->getBehaviorSetting($this->pluginId, 'view_mode', $this->configuration['override_default']);
if ($view_mode != $override_mode || $override_mode == $new_view_mode) {
return;
}
$view_mode = $new_view_mode;
}
}
Members
Name | Modifiers | Type | Description | Overrides |
---|---|---|---|---|
DependencySerializationTrait:: |
protected | property | An array of entity type IDs keyed by the property name of their storages. | |
DependencySerializationTrait:: |
protected | property | An array of service IDs keyed by property name used for serialization. | |
DependencySerializationTrait:: |
public | function | 1 | |
DependencySerializationTrait:: |
public | function | 2 | |
MessengerTrait:: |
protected | property | The messenger. | 29 |
MessengerTrait:: |
public | function | Gets the messenger. | 29 |
MessengerTrait:: |
public | function | Sets the messenger. | |
ParagraphsBehaviorBase:: |
protected | property | The entity field manager. | |
ParagraphsBehaviorBase:: |
public | function |
Calculates dependencies for the configured plugin. Overrides DependentPluginInterface:: |
|
ParagraphsBehaviorBase:: |
protected | function | Removes default behavior form values before storing the user-set ones. | |
ParagraphsBehaviorBase:: |
public | function |
Gets this plugin's configuration. Overrides ConfigurableInterface:: |
|
ParagraphsBehaviorBase:: |
public | function |
Returns list of field names for the given paragraph type and field type. Overrides ParagraphsBehaviorInterface:: |
|
ParagraphsBehaviorBase:: |
public static | function |
Returns if the plugin can be used for the provided Paragraphs type. Overrides ParagraphsBehaviorInterface:: |
1 |
ParagraphsBehaviorBase:: |
public | function |
Adds a default set of helper variables for preprocessors and templates. Overrides ParagraphsBehaviorInterface:: |
|
ParagraphsBehaviorBase:: |
public | function |
Sets the configuration for this plugin instance. Overrides ConfigurableInterface:: |
|
ParagraphsBehaviorBase:: |
public | function |
Returns a short info icon for the current behavior settings. Overrides ParagraphsBehaviorInterface:: |
1 |
ParagraphsBehaviorBase:: |
public | function |
Returns a short summary for the current behavior settings. Overrides ParagraphsBehaviorInterface:: |
2 |
ParagraphsBehaviorBase:: |
public | function |
Validates the behavior fields form. Overrides ParagraphsBehaviorInterface:: |
1 |
ParagraphsViewmodeBehavior:: |
protected | property | The entity Display Repository. | |
ParagraphsViewmodeBehavior:: |
protected | property | The entity type manager. | |
ParagraphsViewmodeBehavior:: |
public | function |
Builds a behavior perspective for each paragraph based on its type. Overrides ParagraphsBehaviorBase:: |
|
ParagraphsViewmodeBehavior:: |
public | function |
Form constructor. Overrides ParagraphsBehaviorBase:: |
|
ParagraphsViewmodeBehavior:: |
public static | function |
Creates an instance of the plugin. Overrides ParagraphsBehaviorBase:: |
|
ParagraphsViewmodeBehavior:: |
public | function |
Gets default configuration for this plugin. Overrides ParagraphsBehaviorBase:: |
|
ParagraphsViewmodeBehavior:: |
public | function |
Allow plugin to alter the paragraph view mode. Overrides ParagraphsViewmodeBehaviorInterface:: |
|
ParagraphsViewmodeBehavior:: |
public | function |
Submit the values taken from the form to store the values. Overrides ParagraphsBehaviorBase:: |
|
ParagraphsViewmodeBehavior:: |
public | function |
Form submission handler. Overrides ParagraphsBehaviorBase:: |
|
ParagraphsViewmodeBehavior:: |
public | function |
Form validation handler. Overrides ParagraphsBehaviorBase:: |
|
ParagraphsViewmodeBehavior:: |
public | function |
Extends the paragraph render array with behavior. Overrides ParagraphsBehaviorInterface:: |
|
ParagraphsViewmodeBehavior:: |
public | function |
Constructs a ParagraphsViewmodeBehavior object. Overrides ParagraphsBehaviorBase:: |
|
PluginBase:: |
protected | property | Configuration information passed into the plugin. | 1 |
PluginBase:: |
protected | property | The plugin implementation definition. | 1 |
PluginBase:: |
protected | property | The plugin_id. | |
PluginBase:: |
constant | A string which is used to separate base plugin IDs from the derivative ID. | ||
PluginBase:: |
public | function |
Gets the base_plugin_id of the plugin instance. Overrides DerivativeInspectionInterface:: |
|
PluginBase:: |
public | function |
Gets the derivative_id of the plugin instance. Overrides DerivativeInspectionInterface:: |
|
PluginBase:: |
public | function |
Gets the definition of the plugin implementation. Overrides PluginInspectionInterface:: |
3 |
PluginBase:: |
public | function |
Gets the plugin_id of the plugin instance. Overrides PluginInspectionInterface:: |
|
PluginBase:: |
public | function | Determines if the plugin is configurable. | |
StringTranslationTrait:: |
protected | property | The string translation service. | 1 |
StringTranslationTrait:: |
protected | function | Formats a string containing a count of items. | |
StringTranslationTrait:: |
protected | function | Returns the number of plurals supported by a given language. | |
StringTranslationTrait:: |
protected | function | Gets the string translation service. | |
StringTranslationTrait:: |
public | function | Sets the string translation service to use. | 2 |
StringTranslationTrait:: |
protected | function | Translates a string to the current language or to a given language. |