getDefaultSectionStorage example

protected function buildMessage(EntityInterface $entity, OverridesSectionStorageInterface $section_storage) {
    $entity_type = $entity->getEntityType();
    $bundle_info = $this->entityTypeBundleInfo->getBundleInfo($entity->getEntityTypeId());

    $variables = [
      '@bundle' => $bundle_info[$entity->bundle()]['label'],
      '@singular_label' => $entity_type->getSingularLabel(),
      '@plural_label' => $entity_type->getPluralLabel(),
    ];

    $defaults_link = $section_storage
      ->getDefaultSectionStorage()
      ->getLayoutBuilderUrl();

    if ($defaults_link->access($this->currentUser())) {
      $variables[':link'] = $defaults_link->toString();
      if ($entity_type->hasKey('bundle')) {
        $message = $this->t('You are editing the layout for this @bundle @singular_label. <a href=":link">Edit the template for all @bundle @plural_label instead.</a>', $variables);
      }
      else {
        $message = $this->t('You are editing the layout for this @singular_label. <a href=":link">Edit the template for all @plural_label instead.</a>', $variables);
      }
    }
    
$this->assertSame($context$result['layout_builder.entity']);
  }

  /** * @covers ::getDefaultSectionStorage */
  public function testGetDefaultSectionStorage() {
    $entity = EntityTest::create();
    $entity->save();
    $this->plugin->setContext('entity', EntityContext::fromEntity($entity));
    $this->plugin->setContext('view_mode', new Context(ContextDefinition::create('string'), 'default'));
    $this->assertInstanceOf(DefaultsSectionStorageInterface::class$this->plugin->getDefaultSectionStorage());
  }

  /** * @covers ::getTempstoreKey */
  public function testGetTempstoreKey() {
    $entity = EntityTest::create();
    $entity->save();
    $this->plugin->setContext('entity', EntityContext::fromEntity($entity));
    $this->plugin->setContext('view_mode', new Context(new ContextDefinition('string'), 'default'));

    
    $bundle_access = AccessResult::allowedIfHasPermission($account, "configure all {$entity->bundle()} {$entity->getEntityTypeId()} layout overrides");
    // 3. The user can configure layouts items of this bundle type they can edit     // AND the user has access to edit this entity.     $edit_only_bundle_access = AccessResult::allowedIfHasPermission($account, "configure editable {$entity->bundle()} {$entity->getEntityTypeId()} layout overrides");
    $edit_only_bundle_access = $edit_only_bundle_access->andIf($entity->access('update', $account, TRUE));

    $result = $any_access
      ->orIf($bundle_access)
      ->orIf($edit_only_bundle_access);

    // Access also depends on the default being enabled.     $result = $result->andIf($this->getDefaultSectionStorage()->access($operation$account, TRUE));
    $result = $this->handleTranslationAccess($result$operation$account);
    return $return_as_object ? $result : $result->isAllowed();
  }

  /** * Handles access checks related to translations. * * @param \Drupal\Core\Access\AccessResult $result * The access result. * @param string $operation * The operation to be performed. * @param \Drupal\Core\Session\AccountInterface $account * The user for which to check access. * * @return \Drupal\Core\Access\AccessResultInterface * The access result. */
public function onPrepareLayout(PrepareLayoutEvent $event) {
    $section_storage = $event->getSectionStorage();

    // If the layout has pending changes, add a warning.     if ($this->layoutTempstoreRepository->has($section_storage)) {
      $this->messenger->addWarning($this->t('You have unsaved changes.'));
    }
    else {
      // If the layout is an override that has not yet been overridden, copy the       // sections from the corresponding default.       if ($section_storage instanceof OverridesSectionStorageInterface && !$section_storage->isOverridden()) {
        $sections = $section_storage->getDefaultSectionStorage()->getSections();
        foreach ($sections as $section) {
          $section_storage->appendSection($section);
        }
      }
      // Add storage to tempstore regardless of what the storage is.       $this->layoutTempstoreRepository->set($section_storage);
    }
  }

}
Home | Imprint | This part of the site doesn't use cookies.