<?php
namespace Drupal\paragraphs_collection\Plugin\paragraphs\Behavior;
use Drupal\Core\Access\AccessResult;
use Drupal\Core\Entity\Display\EntityViewDisplayInterface;
use Drupal\Core\Entity\EntityFieldManager;
use Drupal\Core\Extension\ModuleHandlerInterface;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Language\LanguageManagerInterface;
use Drupal\Core\Session\AccountInterface;
use Drupal\paragraphs\Entity\Paragraph;
use Drupal\paragraphs\ParagraphInterface;
use Drupal\paragraphs\ParagraphsBehaviorBase;
use Symfony\Component\DependencyInjection\ContainerInterface;
class ParagraphsLanguagePlugin extends ParagraphsBehaviorBase {
protected $languageManager;
protected $moduleHandler;
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;
}
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'));
}
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;
}
public static function determineParagraphAccess(ParagraphInterface $paragraph, $operation, AccountInterface $account) {
$access_result = AccessResult::neutral();
$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();
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);
}
public function view(array &$build, Paragraph $paragraphs_entity, EntityViewDisplayInterface $display, $view_mode) {
}
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;
}
}