You are here

class LayoutParagraphsBuilderAccess in Layout Paragraphs 2.0.x

Class definition for LayoutParagraphsBuilderAccess.

Checks access to layout paragraphs builder instance.

Hierarchy

Expanded class hierarchy of LayoutParagraphsBuilderAccess

1 string reference to 'LayoutParagraphsBuilderAccess'
layout_paragraphs.services.yml in ./layout_paragraphs.services.yml
layout_paragraphs.services.yml
1 service uses LayoutParagraphsBuilderAccess
layout_paragraphs.builder_access in ./layout_paragraphs.services.yml
Drupal\layout_paragraphs\Access\LayoutParagraphsBuilderAccess

File

src/Access/LayoutParagraphsBuilderAccess.php, line 16

Namespace

Drupal\layout_paragraphs\Access
View source
class LayoutParagraphsBuilderAccess implements AccessInterface {

  /**
   * The entity type manager service.
   *
   * @var \Drupal\Core\Entity\EntityTypeManagerInterface
   */
  protected $entityTypeManager;

  /**
   * Class constructor.
   *
   * @param \Drupal\Core\Entity\EntityTypeManagerInterface $entity_type_manager
   *   The entity type manager service.
   */
  public function __construct(EntityTypeManagerInterface $entity_type_manager) {
    $this->entityTypeManager = $entity_type_manager;
  }

  /**
   * {@inheritDoc}
   *
   * @param \Drupal\Core\Session\AccountInterface $account
   *   The account to check access for.
   * @param \Drupal\layout_paragraphs\LayoutParagraphsLayout $layout_paragraphs_layout
   *   The layout paragraphs layout object.
   * @param string $operation
   *   The operation being peformed (i.e. 'create' or 'delete').
   * @param string $component_uuid
   *   The specific component being acted on.
   * @param \Drupal\paragraphs\ParagraphsTypeInterface $paragraph_type
   *   The paragraph type of a component being added.
   *
   * @return \Drupal\Core\Access\AccessResult
   *   The access result.
   */
  public function access(AccountInterface $account, LayoutParagraphsLayout $layout_paragraphs_layout, string $operation = NULL, string $component_uuid = NULL, ParagraphsTypeInterface $paragraph_type = NULL) {

    // Check field access.
    $access = $layout_paragraphs_layout
      ->getParagraphsReferenceField()
      ->access('edit', $account, TRUE);

    // Check access to host entity.
    $entity = $layout_paragraphs_layout
      ->getEntity();
    $lp_operation = $entity
      ->isNew() ? 'create' : 'update';
    $access = $access
      ->andIf($entity
      ->access($lp_operation, $account, TRUE));

    // Check access to specific paragraph entity.
    if ($component_uuid) {
      $paragraph = $layout_paragraphs_layout
        ->getComponentByUuid($component_uuid)
        ->getEntity();
      $access = $access
        ->andIf($paragraph
        ->access($operation, $account, TRUE));
    }

    // Check access to paragraph type.
    if ($paragraph_type) {
      $access_handler = $this->entityTypeManager
        ->getAccessControlHandler('paragraph');
      $access = $access
        ->andIf($access_handler
        ->createAccess($paragraph_type
        ->id(), $account, [], TRUE));
    }
    return $access;
  }

}

Members

Namesort descending Modifiers Type Description Overrides
LayoutParagraphsBuilderAccess::$entityTypeManager protected property The entity type manager service.
LayoutParagraphsBuilderAccess::access public function
LayoutParagraphsBuilderAccess::__construct public function Class constructor.