You are here

public function RemoveEntityFromViewEventSubscriber::onEntityFieldAccessDenied in Permissions by Term 8.2

Same name and namespace in other branches
  1. 8 modules/permissions_by_entity/src/EventSubscriber/RemoveEntityFromViewEventSubscriber.php \Drupal\permissions_by_entity\EventSubscriber\RemoveEntityFromViewEventSubscriber::onEntityFieldAccessDenied()

Callback method.

Callback method that will be called when the ENTITY_FIELD_VALUE_ACCESS_DENIED_EVENT has been triggered.

Parameters

\Drupal\permissions_by_entity\Event\EntityFieldValueAccessDeniedEvent $event: The event.

File

modules/permissions_by_entity/src/EventSubscriber/RemoveEntityFromViewEventSubscriber.php, line 37

Class

RemoveEntityFromViewEventSubscriber
Class RemoveEntityFromViewEventSubscriber.

Namespace

Drupal\permissions_by_entity\EventSubscriber

Code

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);
}