You are here

class RemoveEntityFromViewEventSubscriber 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

Class RemoveEntityFromViewEventSubscriber.

@package Drupal\permissions_by_entity\EventSubscriber

Hierarchy

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
permissions_by_entity.remove_entity_from_view_event_subscriber in modules/permissions_by_entity/permissions_by_entity.services.yml
Drupal\permissions_by_entity\EventSubscriber\RemoveEntityFromViewEventSubscriber

File

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

Namespace

Drupal\permissions_by_entity\EventSubscriber
View 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

Namesort descending Modifiers Type Description Overrides
RemoveEntityFromViewEventSubscriber::getSubscribedEvents public static function Returns an array of event names this subscriber wants to listen to.
RemoveEntityFromViewEventSubscriber::onEntityFieldAccessDenied public function Callback method.