class RemoveEntityFromViewEventSubscriber in Permissions by Term 8
Same name and namespace in other branches
- 8.2 modules/permissions_by_entity/src/EventSubscriber/RemoveEntityFromViewEventSubscriber.php \Drupal\permissions_by_entity\EventSubscriber\RemoveEntityFromViewEventSubscriber
Class RemoveEntityFromViewEventSubscriber.
@package Drupal\permissions_by_entity\EventSubscriber
Hierarchy
- class \Drupal\permissions_by_entity\EventSubscriber\RemoveEntityFromViewEventSubscriber implements \Symfony\Component\EventDispatcher\EventSubscriberInterface
Expanded class hierarchy of RemoveEntityFromViewEventSubscriber
1 string reference to 'RemoveEntityFromViewEventSubscriber'
- permissions_by_entity.services.yml in modules/
permissions_by_entity/ permissions_by_entity.services.yml - modules/permissions_by_entity/permissions_by_entity.services.yml
1 service uses RemoveEntityFromViewEventSubscriber
File
- modules/
permissions_by_entity/ src/ EventSubscriber/ RemoveEntityFromViewEventSubscriber.php, line 14
Namespace
Drupal\permissions_by_entity\EventSubscriberView source
class RemoveEntityFromViewEventSubscriber implements EventSubscriberInterface {
/**
* {@inheritdoc}
*/
public static function getSubscribedEvents() {
return [
PermissionsByEntityEvents::ENTITY_FIELD_VALUE_ACCESS_DENIED_EVENT => [
'onEntityFieldAccessDenied',
],
];
}
/**
* Callback method.
*
* Callback method that will be called when the
* ENTITY_FIELD_VALUE_ACCESS_DENIED_EVENT has been triggered.
*
* @param \Drupal\permissions_by_entity\Event\EntityFieldValueAccessDeniedEvent $event
* The event.
*/
public function onEntityFieldAccessDenied(EntityFieldValueAccessDeniedEvent $event) {
// Get the field.
$field = $event
->getField();
// Get the number of values this field contains.
$num_values = $field
->count();
// Get the current value of the field.
$field_values = $field
->getValue();
// Iterate over the values.
for ($i = 0; $i < $num_values; $i++) {
$field_entity = $field
->get($i)->entity;
// If the entity matches the entity of the event.
if ($field_entity === $event
->getEntity()) {
// Remove the this value from the values array.
unset($field_values[$i]);
// We need to decrement the current index.
$event
->setIndex($event
->getIndex() - 1);
}
}
// Set the field values.
$field
->setValue($field_values);
}
}
Members
Name | Modifiers | Type | Description | Overrides |
---|---|---|---|---|
RemoveEntityFromViewEventSubscriber:: |
public static | function | Returns an array of event names this subscriber wants to listen to. | |
RemoveEntityFromViewEventSubscriber:: |
public | function | Callback method. |