class ParagraphsLanguagePlugin in Paragraphs Collection 8
Provides a way to hide specific paragraphs depending on the current language.
Plugin annotation
@ParagraphsBehavior(
id = "language",
label = @Translation("Visibility per language"),
description = @Translation("Restricts visibility of a paragraph per language. Usage on children of a container paragraph which uses a container behavior like Grid layout can have unexpected visual results."),
weight = 0
)
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_collection\Plugin\paragraphs\Behavior\ParagraphsLanguagePlugin
- class \Drupal\paragraphs\ParagraphsBehaviorBase implements ContainerFactoryPluginInterface, ParagraphsBehaviorInterface
- class \Drupal\Core\Plugin\PluginBase uses DependencySerializationTrait, MessengerTrait, StringTranslationTrait
Expanded class hierarchy of ParagraphsLanguagePlugin
1 file declares its use of ParagraphsLanguagePlugin
File
- src/
Plugin/ paragraphs/ Behavior/ ParagraphsLanguagePlugin.php, line 27
Namespace
Drupal\paragraphs_collection\Plugin\paragraphs\BehaviorView source
class ParagraphsLanguagePlugin extends ParagraphsBehaviorBase {
/**
* The language manager.
*
* @var \Drupal\Core\Language\LanguageManagerInterface
*/
protected $languageManager;
/**
* The module handler.
*/
protected $moduleHandler;
/**
* ParagraphsLanguagePlugin constructor.
*
* @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\Language\LanguageManagerInterface $language_manager
* The language manager.
* @param \Drupal\Core\Extension\ModuleHandlerInterface $module_handler
* The module handler.
*/
public function __construct(array $configuration, $plugin_id, $plugin_definition, EntityFieldManager $entity_field_manager, LanguageManagerInterface $language_manager, ModuleHandlerInterface $module_handler) {
parent::__construct($configuration, $plugin_id, $plugin_definition, $entity_field_manager);
$this->languageManager = $language_manager;
$this->moduleHandler = $module_handler;
}
/**
* {@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('language_manager'), $container
->get('module_handler'));
}
/**
* {@inheritdoc}
*/
public function buildBehaviorForm(ParagraphInterface $paragraph, array &$form, FormStateInterface $form_state) {
if (!$this->languageManager
->isMultilingual()) {
return [];
}
foreach ($this->languageManager
->getLanguages() as $language_code => $language) {
$options[$language_code] = $language
->getName();
}
$form['container'] = [
'#type' => 'container',
'#attributes' => [
'class' => [
'paragraphs-plugin-inline-container',
],
],
];
$form['container']['visibility'] = [
'#type' => 'select',
'#title' => $this
->t('Language visibility'),
'#options' => [
'always' => $this
->t('- Always visible -'),
'hide' => $this
->t('Hide for'),
'show' => $this
->t('Show for'),
],
'#default_value' => $paragraph
->getBehaviorSetting($this
->getPluginId(), [
'container',
'visibility',
]),
'#multiple' => FALSE,
'#attributes' => [
'id' => [
'paragraphs-behavior-language-behavior-form-visibility-' . $paragraph
->id(),
],
'class' => [
'paragraphs-plugin-form-element',
],
],
];
$use_select2 = $this->moduleHandler
->moduleExists('select2');
$form['container']['languages'] = [
'#type' => $use_select2 ? 'select2' : 'select',
'#options' => $options,
'#empty_option' => $this
->t('- None -'),
'#empty_value' => 'none',
'#default_value' => $paragraph
->getBehaviorSetting($this
->getPluginId(), [
'container',
'languages',
]),
'#states' => [
'invisible' => [
':input[id="paragraphs-behavior-language-behavior-form-visibility-' . $paragraph
->id() . '"]' => [
'value' => 'always',
],
],
],
'#multiple' => TRUE,
'#attributes' => [
'class' => [
'paragraphs-behavior-language-behavior-form-languages',
'paragraphs-plugin-form-element',
],
],
];
if ($use_select2) {
$form['container']['languages']['#select2']['width'] = 'auto';
}
$form['#attached']['library'][] = 'paragraphs_collection/plugin_admin';
$form['container']['#attributes']['class'][] = 'paragraphs-behavior-language-behavior-form';
return $form;
}
/**
* Check the access for the paragraph based on the visibility setting.
*
* @param \Drupal\paragraphs\ParagraphInterface $paragraph
* The paragraph entity.
* @param string $operation
* The operation.
* @param \Drupal\Core\Session\AccountInterface $account
* The logged in user.
*
* @return \Drupal\Core\Access\AccessResult
* The access result.
*/
public static function determineParagraphAccess(ParagraphInterface $paragraph, $operation, AccountInterface $account) {
$access_result = AccessResult::neutral();
/** @var \Drupal\paragraphs\Entity\ParagraphsType $type */
$type = $paragraph
->getParagraphType();
if ($operation === 'view' && $type
->hasEnabledBehaviorPlugin('language')) {
$visibility = $paragraph
->getBehaviorSetting('language', [
'container',
'visibility',
]);
if (in_array($visibility, [
'show',
'hide',
], TRUE)) {
$languages = $paragraph
->getBehaviorSetting('language', [
'container',
'languages',
]) ?: [];
$current_language = \Drupal::languageManager()
->getCurrentLanguage();
// In the 'show' visibility mode: Hide the paragraph, if the current
// language is not among the selected ones.
if ($visibility == 'show') {
$access_result = AccessResult::forbiddenIf(!in_array($current_language
->getId(), $languages));
}
else {
$access_result = AccessResult::forbiddenIf(in_array($current_language
->getId(), $languages));
}
}
}
return $access_result
->addCacheableDependency($paragraph)
->addCacheableDependency($type);
}
/**
* {@inheritdoc}
*/
public function view(array &$build, Paragraph $paragraphs_entity, EntityViewDisplayInterface $display, $view_mode) {
// Do nothing.
}
/**
* {@inheritdoc}
*/
public function settingsSummary(Paragraph $paragraph) {
$summary = [];
if ($visibility = $paragraph
->getBehaviorSetting($this
->getPluginId(), 'container')) {
if ($visibility['visibility'] == 'hide' || $visibility['visibility'] == 'show') {
if (isset($visibility['languages'])) {
$language_names = [];
foreach ($visibility['languages'] as $language) {
$language_names[] = $this->languageManager
->getLanguageName($language);
}
$summary[] = [
'label' => $visibility['visibility'] == 'hide' ? $this
->t('Hide for') : $this
->t('Show for'),
'value' => \implode(', ', $language_names),
];
}
}
}
return $summary;
}
}
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 |
Form constructor. Overrides PluginFormInterface:: |
2 |
ParagraphsBehaviorBase:: |
public | function |
Calculates dependencies for the configured plugin. Overrides DependentPluginInterface:: |
|
ParagraphsBehaviorBase:: |
public | function |
Gets default configuration for this plugin. Overrides ConfigurableInterface:: |
2 |
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 |
Submit the values taken from the form to store the values. Overrides ParagraphsBehaviorInterface:: |
|
ParagraphsBehaviorBase:: |
public | function |
Form submission handler. Overrides PluginFormInterface:: |
1 |
ParagraphsBehaviorBase:: |
public | function |
Validates the behavior fields form. Overrides ParagraphsBehaviorInterface:: |
1 |
ParagraphsBehaviorBase:: |
public | function |
Form validation handler. Overrides PluginFormInterface:: |
1 |
ParagraphsLanguagePlugin:: |
protected | property | The language manager. | |
ParagraphsLanguagePlugin:: |
protected | property | The module handler. | |
ParagraphsLanguagePlugin:: |
public | function |
Builds a behavior perspective for each paragraph based on its type. Overrides ParagraphsBehaviorBase:: |
|
ParagraphsLanguagePlugin:: |
public static | function |
Creates an instance of the plugin. Overrides ParagraphsBehaviorBase:: |
|
ParagraphsLanguagePlugin:: |
public static | function | Check the access for the paragraph based on the visibility setting. | |
ParagraphsLanguagePlugin:: |
public | function |
Returns a short summary for the current behavior settings. Overrides ParagraphsBehaviorBase:: |
|
ParagraphsLanguagePlugin:: |
public | function |
Extends the paragraph render array with behavior. Overrides ParagraphsBehaviorInterface:: |
|
ParagraphsLanguagePlugin:: |
public | function |
ParagraphsLanguagePlugin constructor. 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. |