You are here

public static function ParagraphsLanguagePlugin::determineParagraphAccess in Paragraphs Collection 8

Check the access for the paragraph based on the visibility setting.

Parameters

\Drupal\paragraphs\ParagraphInterface $paragraph: The paragraph entity.

string $operation: The operation.

\Drupal\Core\Session\AccountInterface $account: The logged in user.

Return value

\Drupal\Core\Access\AccessResult The access result.

1 call to ParagraphsLanguagePlugin::determineParagraphAccess()
paragraphs_collection_paragraph_access in ./paragraphs_collection.module
Implements hook_ENTITY_TYPE_access().

File

src/Plugin/paragraphs/Behavior/ParagraphsLanguagePlugin.php, line 151

Class

ParagraphsLanguagePlugin
Provides a way to hide specific paragraphs depending on the current language.

Namespace

Drupal\paragraphs_collection\Plugin\paragraphs\Behavior

Code

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);
}