class EntityAccessByFieldPermissions in Open Social 8.2
Same name and namespace in other branches
- 8.9 modules/custom/entity_access_by_field/src/EntityAccessByFieldPermissions.php \Drupal\entity_access_by_field\EntityAccessByFieldPermissions
- 8 modules/custom/entity_access_by_field/src/EntityAccessByFieldPermissions.php \Drupal\entity_access_by_field\EntityAccessByFieldPermissions
- 8.3 modules/custom/entity_access_by_field/src/EntityAccessByFieldPermissions.php \Drupal\entity_access_by_field\EntityAccessByFieldPermissions
- 8.4 modules/custom/entity_access_by_field/src/EntityAccessByFieldPermissions.php \Drupal\entity_access_by_field\EntityAccessByFieldPermissions
- 8.5 modules/custom/entity_access_by_field/src/EntityAccessByFieldPermissions.php \Drupal\entity_access_by_field\EntityAccessByFieldPermissions
- 8.6 modules/custom/entity_access_by_field/src/EntityAccessByFieldPermissions.php \Drupal\entity_access_by_field\EntityAccessByFieldPermissions
- 8.7 modules/custom/entity_access_by_field/src/EntityAccessByFieldPermissions.php \Drupal\entity_access_by_field\EntityAccessByFieldPermissions
- 8.8 modules/custom/entity_access_by_field/src/EntityAccessByFieldPermissions.php \Drupal\entity_access_by_field\EntityAccessByFieldPermissions
- 10.3.x modules/custom/entity_access_by_field/src/EntityAccessByFieldPermissions.php \Drupal\entity_access_by_field\EntityAccessByFieldPermissions
- 10.0.x modules/custom/entity_access_by_field/src/EntityAccessByFieldPermissions.php \Drupal\entity_access_by_field\EntityAccessByFieldPermissions
- 10.1.x modules/custom/entity_access_by_field/src/EntityAccessByFieldPermissions.php \Drupal\entity_access_by_field\EntityAccessByFieldPermissions
- 10.2.x modules/custom/entity_access_by_field/src/EntityAccessByFieldPermissions.php \Drupal\entity_access_by_field\EntityAccessByFieldPermissions
EntityAccessByFieldPermissions.
Hierarchy
- class \Drupal\entity_access_by_field\EntityAccessByFieldPermissions implements ContainerInjectionInterface uses UrlGeneratorTrait, StringTranslationTrait
Expanded class hierarchy of EntityAccessByFieldPermissions
1 string reference to 'EntityAccessByFieldPermissions'
- entity_access_by_field.services.yml in modules/
custom/ entity_access_by_field/ entity_access_by_field.services.yml - modules/custom/entity_access_by_field/entity_access_by_field.services.yml
1 service uses EntityAccessByFieldPermissions
- entity_access_by_field.permissions in modules/
custom/ entity_access_by_field/ entity_access_by_field.services.yml - Drupal\entity_access_by_field\EntityAccessByFieldPermissions
File
- modules/
custom/ entity_access_by_field/ src/ EntityAccessByFieldPermissions.php, line 14
Namespace
Drupal\entity_access_by_fieldView source
class EntityAccessByFieldPermissions implements ContainerInjectionInterface {
use StringTranslationTrait;
use UrlGeneratorTrait;
/**
* The entity manager.
*
* @var \Drupal\Core\Entity\EntityManagerInterface
*/
protected $entityManager;
/**
* Constructs a new NodeViewPermissionsPermission instance.
*
* @param \Drupal\Core\Entity\EntityManagerInterface $entity_manager
* The entity manager.
*/
public function __construct(EntityManagerInterface $entity_manager) {
$this->entityManager = $entity_manager;
}
/**
* {@inheritdoc}
*/
public static function create(ContainerInterface $container) {
return new static($container
->get('entity.manager'));
}
/**
* Array with values which need to be ignored.
*
* @return array
* An array containing a list of values to ignore.
*/
public static function getIgnoredValues() {
return EntityAccessHelper::getIgnoredValues();
}
/**
* Return all the permissions generated by this user.
*/
public function permissions() {
$permissions = [];
$contentTypes = $this
->getContentTypes();
foreach ($contentTypes as $bundle) {
$entity_type = 'node';
$fields = $this
->getEntityAccessFields($entity_type, $bundle);
/** @var \Drupal\field\Entity\FieldConfig $field */
foreach ($fields as $field) {
$field_storage = $field
->getFieldStorageDefinition();
// @TODO Add support for allowed_values_function.
$allowed_values = $field_storage
->getSetting('allowed_values');
if (!empty($allowed_values)) {
foreach ($allowed_values as $field_key => $field_label) {
if (!in_array($field_key, $this
->getIgnoredValues())) {
// e.g. label = node.article.field_content_visibility:public.
$permission_label = $field
->id() . ':' . $field_key;
$permission = 'view ' . $permission_label . ' content';
$permissions[$permission] = [
'title' => $this
->t('View @label content', [
'@label' => $permission_label,
]),
];
}
}
}
}
}
return $permissions;
}
/**
* Get the realms array with permissions as value.
*/
public function getRealmWithPermission() {
$realms = [];
$contentTypes = $this
->getContentTypes();
foreach ($contentTypes as $bundle) {
$entity_type = 'node';
$fields = $this
->getEntityAccessFields($entity_type, $bundle);
/** @var \Drupal\field\Entity\FieldConfig $field */
foreach ($fields as $field) {
$field_storage = $field
->getFieldStorageDefinition();
// @TODO Add support for allowed_values_function.
$allowed_values = $field_storage
->getSetting('allowed_values');
if (!empty($allowed_values)) {
foreach ($allowed_values as $field_key => $field_label) {
// e.g. label = node.article.field_content_visibility:public.
$permission_label = $field
->id() . ':' . $field_key;
$op = 'view';
$permission = $op . ' ' . $permission_label . ' content';
$bundle_id = $bundle
->id();
$field_name = $field
->getName();
$realm = $this
->getRealmForFieldValue($op, $entity_type, $bundle_id, $field_name, $field_key);
$realms[$realm] = $permission;
}
}
}
}
return $realms;
}
/**
* Returns a realm for a field value in order to create access.
*
* @return string
* The string with the realm created.
*/
public function getRealmForFieldValue($op, $entity_type, $bundle_id, $field_name, $field_value) {
$realm = $op . '_' . $entity_type . '_' . $bundle_id . '_' . $field_name . '_' . $field_value;
return $realm;
}
/**
* Get all the content types.
*
* @return \Drupal\Core\Entity\EntityInterface[]
* Returns the entity interface containing all the content types.
*/
protected function getContentTypes() {
$contentTypes = $this->entityManager
->getStorage('node_type')
->loadMultiple();
return $contentTypes;
}
/**
* Get all fields of type entity_access_field.
*
* @return array
* Returns all the fields with the entity type entity_acces_field.
*/
public function getEntityAccessFields($entity, $bundle) {
$fields = [];
$field_definitions = $this->entityManager
->getFieldDefinitions($entity, $bundle
->id());
foreach ($field_definitions as $field_name => $field_definition) {
if (!empty($field_definition
->getTargetBundle())) {
if ($field_definition
->getType() === 'entity_access_field') {
$fields[$field_name] = $field_definition;
}
}
}
return $fields;
}
}
Members
Name | Modifiers | Type | Description | Overrides |
---|---|---|---|---|
EntityAccessByFieldPermissions:: |
protected | property | The entity manager. | |
EntityAccessByFieldPermissions:: |
public static | function |
Instantiates a new instance of this class. Overrides ContainerInjectionInterface:: |
|
EntityAccessByFieldPermissions:: |
protected | function | Get all the content types. | |
EntityAccessByFieldPermissions:: |
public | function | Get all fields of type entity_access_field. | |
EntityAccessByFieldPermissions:: |
public static | function | Array with values which need to be ignored. | |
EntityAccessByFieldPermissions:: |
public | function | Returns a realm for a field value in order to create access. | |
EntityAccessByFieldPermissions:: |
public | function | Get the realms array with permissions as value. | |
EntityAccessByFieldPermissions:: |
public | function | Return all the permissions generated by this user. | |
EntityAccessByFieldPermissions:: |
public | function | Constructs a new NodeViewPermissionsPermission instance. | |
StringTranslationTrait:: |
protected | property | The string translation service. | 1 |
StringTranslationTrait:: |
protected | function | Formats a string containing a count of items. | |
StringTranslationTrait:: |
protected | function | Returns the number of plurals supported by a given language. | |
StringTranslationTrait:: |
protected | function | Gets the string translation service. | |
StringTranslationTrait:: |
public | function | Sets the string translation service to use. | 2 |
StringTranslationTrait:: |
protected | function | Translates a string to the current language or to a given language. | |
UrlGeneratorTrait:: |
protected | property | The url generator. | |
UrlGeneratorTrait:: |
protected | function | Returns the URL generator service. | |
UrlGeneratorTrait:: |
protected | function | Returns a redirect response object for the specified route. | 3 |
UrlGeneratorTrait:: |
public | function | Sets the URL generator service. | |
UrlGeneratorTrait:: |
protected | function | Generates a URL or path for a specific route based on the given parameters. |