inheritCacheability example


function hook_entity_field_access_alter(array &$grants, array $context) {
  /** @var \Drupal\Core\Field\FieldDefinitionInterface $field_definition */
  $field_definition = $context['field_definition'];
  if ($field_definition->getName() == 'field_of_interest' && $grants['node']->isForbidden()) {
    // Override node module's restriction to no opinion (neither allowed nor     // forbidden). We don't want to provide our own access hook, we only want to     // take out node module's part in the access handling of this field. We also     // don't want to switch node module's grant to     // AccessResultInterface::isAllowed() , because the grants of other modules     // should still decide on their own if this field is accessible or not     $grants['node'] = AccessResult::neutral()->inheritCacheability($grants['node']);
  }
}

/** * Acts when initializing a fieldable entity object. * * This hook runs after a new entity object or a new entity translation object * has just been instantiated. It can be used to set initial values, e.g. to * provide defaults. * * @param \Drupal\Core\Entity\FieldableEntityInterface $entity * The entity object. * * @ingroup entity_crud * @see hook_ENTITY_TYPE_field_values_init() */
if (!$this->isNeutral() || ($this->getCacheMaxAge() === 0 && $other->isNeutral()) || ($this->getCacheMaxAge() !== 0 && $other instanceof CacheableDependencyInterface && $other->getCacheMaxAge() !== 0)) {
        $merge_other = TRUE;
      }

      if ($this instanceof AccessResultReasonInterface && $this->getReason() !== '') {
        $result->setReason($this->getReason());
      }
      elseif ($other instanceof AccessResultReasonInterface && $other->getReason() !== '') {
        $result->setReason($other->getReason());
      }
    }
    $result->inheritCacheability($this);
    if ($merge_other) {
      $result->inheritCacheability($other);
    }
    return $result;
  }

  /** * {@inheritdoc} */
  public function andIf(AccessResultInterface $other) {
    // The other access result's cacheability metadata is merged if $merge_other
// Get entity access callback.       $definition = $this->entityTypeManager->getDefinition($entity_type_id);
      $translation = $definition->get('translation');
      $access_callback = $translation['content_translation']['access_callback'];
      $access = call_user_func($access_callback$entity);
      if ($access->isAllowed()) {
        return $access;
      }

      // Check "translate any entity" permission.       if ($account->hasPermission('translate any entity')) {
        return AccessResult::allowed()->cachePerPermissions()->inheritCacheability($access);
      }

      // Check per entity permission.       $permission = "translate {$entity_type_id}";
      if ($definition->getPermissionGranularity() == 'bundle') {
        $permission = "translate {$bundle} {$entity_type_id}";
      }
      return AccessResult::allowedIfHasPermission($account$permission)->inheritCacheability($access);
    }

    // No opinion.
$non_cacheable = AccessResult::neutral()->addCacheableDependency($non_cacheable_dependency);
    $verify($non_cacheable[][], 0);
  }

  /** * @covers ::inheritCacheability */
  public function testInheritCacheability() {
    // andIf(); 1st has defaults, 2nd has custom tags, contexts and max-age.     $access = AccessResult::allowed();
    $other = AccessResult::allowed()->setCacheMaxAge(1500)->cachePerPermissions()->addCacheTags(['node:20011988']);
    $this->assertInstanceOf(AccessResult::class$access->inheritCacheability($other));
    $this->assertSame(['user.permissions']$access->getCacheContexts());
    $this->assertSame(['node:20011988']$access->getCacheTags());
    $this->assertSame(1500, $access->getCacheMaxAge());

    // andIf(); 1st has custom tags, max-age, 2nd has custom contexts and max-age.     $access = AccessResult::allowed()->cachePerUser()->setCacheMaxAge(43200);
    $other = AccessResult::forbidden()->addCacheTags(['node:14031991'])->setCacheMaxAge(86400);
    $this->assertInstanceOf(AccessResult::class$access->inheritCacheability($other));
    $this->assertSame(['user']$access->getCacheContexts());
    $this->assertSame(['node:14031991']$access->getCacheTags());
    $this->assertSame(43200, $access->getCacheMaxAge());
  }
        $bundle_access_control_handler = $this->entityTypeManager->getAccessControlHandler($entity_type->getBundleEntityType());
        $access = $access->orIf($bundle_access_control_handler->createAccess(NULL, $account[], TRUE));
        if ($access->isAllowed()) {
          return $access;
        }
      }
    }

    // Check whether an entity of any bundle may be created.     foreach ($bundles as $bundle) {
      $bundle_access = $access_control_handler->createAccess($bundle$account[], TRUE);
      $access->inheritCacheability($bundle_access);
      if ($bundle_access instanceof AccessResultReasonInterface && $bundle_access->getReason() !== "" && $access->getReason() === "") {
        $access->setReason($bundle_access->getReason());
      }

      // In case there is at least one bundle the user can create entities for,       // access is allowed.       if ($bundle_access->isAllowed()) {
        return AccessResult::allowed()->inheritCacheability($access);
      }
    }

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