You are here

public function BehaviorInvoker::processEntity in Rabbit Hole 2.x

Same name and namespace in other branches
  1. 8 src/BehaviorInvoker.php \Drupal\rabbit_hole\BehaviorInvoker::processEntity()

Invoke a rabbit hole behavior based on an entity's configuration.

This assumes the entity is configured for use with Rabbit Hole - if you pass an entity to this method and it does not have a rabbit hole plugin it will use the defaults!

Parameters

\Drupal\Core\Entity\ContentEntityInterface $entity: The entity to apply rabbit hole behavior on.

\Symfony\Component\HttpFoundation\Response $current_response: The current response, to be passed along to and potentially altered by any called rabbit hole plugin.

Return value

\Symfony\Component\HttpFoundation\Response|null A response or NULL if the response is unchanged.

Throws

\Drupal\Component\Plugin\Exception\PluginException

\Drupal\Component\Plugin\Exception\PluginNotFoundException

Overrides BehaviorInvokerInterface::processEntity

File

src/BehaviorInvoker.php, line 131

Class

BehaviorInvoker
Default implementation of Rabbit Hole behaviors invoker.

Namespace

Drupal\rabbit_hole

Code

public function processEntity(ContentEntityInterface $entity, Response $current_response = NULL) {
  $plugin = $this
    ->getBehaviorPlugin($entity);
  if ($plugin === NULL) {
    return NULL;
  }
  $resp_use = $plugin
    ->usesResponse();
  $response_required = $resp_use == RabbitHoleBehaviorPluginInterface::USES_RESPONSE_ALWAYS;
  $response_allowed = $resp_use == $response_required || $resp_use == RabbitHoleBehaviorPluginInterface::USES_RESPONSE_SOMETIMES;

  // Most plugins never make use of the response and only run when it's not
  // provided (i.e. on a request event).
  if (!$response_allowed && $current_response == NULL || $response_allowed || $response_required && $current_response != NULL) {
    $response = $plugin
      ->performAction($entity, $current_response);

    // Execute a fallback action until we have correct response object.
    // It allows us to have a chain of fallback actions until we execute the
    // final one.
    while (!$response instanceof Response && is_string($response) && $this->rhBehaviorPluginManager
      ->getDefinition($response, FALSE) !== NULL) {
      $fallback_plugin = $this->rhBehaviorPluginManager
        ->createInstance($response, []);
      $response = $fallback_plugin
        ->performAction($entity, $current_response);
    }

    // Alter the response before it is returned.
    $this->moduleHandler
      ->alter('rabbit_hole_response', $response, $entity);
    return $response;
  }
  else {
    return NULL;
  }
}