<?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\Entity\EntityInterface;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Session\AccountInterface;
use Drupal\Core\Session\AccountProxyInterface;
use Drupal\paragraphs\Entity\Paragraph;
use Drupal\paragraphs\Entity\ParagraphsType;
use Drupal\paragraphs\ParagraphInterface;
use Drupal\paragraphs\ParagraphsBehaviorBase;
use Symfony\Component\DependencyInjection\ContainerInterface;
class ParagraphsLockablePlugin extends ParagraphsBehaviorBase {
protected $currentUser;
public function __construct(array $configuration, $plugin_id, $plugin_definition, EntityFieldManager $entity_field_manager, AccountProxyInterface $currentUser) {
parent::__construct($configuration, $plugin_id, $plugin_definition, $entity_field_manager);
$this->currentUser = $currentUser;
}
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('current_user'));
}
public function buildBehaviorForm(ParagraphInterface $paragraph, array &$form, FormStateInterface $form_state) {
$form['locked'] = [
'#type' => 'checkbox',
'#title' => $this
->t('Lock content'),
'#description' => $this
->t('If selected this paragraph content will be locked for anyone without the appropriate permission.'),
'#default_value' => $paragraph
->getBehaviorSetting($this
->getPluginId(), 'locked'),
'#access' => $this->currentUser
->hasPermission('administer lockable paragraph'),
];
return $form;
}
public function view(array &$build, Paragraph $paragraphs_entity, EntityViewDisplayInterface $display, $view_mode) {
}
public static function determineParagraphAccess(EntityInterface $entity, $operation, AccountInterface $account) {
$paragraphs_type = $entity
->getParagraphType();
if ($operation !== 'view' && $entity
->getBehaviorSetting('lockable', 'locked') && $paragraphs_type
->hasEnabledBehaviorPlugin('lockable')) {
$accessResult = AccessResult::forbiddenIf(!$account
->hasPermission('administer lockable paragraph'));
}
else {
$accessResult = AccessResult::neutral();
}
$accessResult
->addCacheableDependency($entity);
$accessResult
->addCacheableDependency($paragraphs_type);
return $accessResult;
}
public function settingsSummary(Paragraph $paragraph) {
$locked = $paragraph
->getBehaviorSetting($this
->getPluginId(), 'locked');
$summary = [
[
'value' => $this
->t('Locked'),
],
];
return $locked ? $summary : [];
}
}