You are here

public function ViewmodepagePattern::applies in View Mode Page 3.2.x

Same name and namespace in other branches
  1. 8.3 src/Entity/ViewmodepagePattern.php \Drupal\view_mode_page\Entity\ViewmodepagePattern::applies()
  2. 4.0.x src/Entity/ViewmodepagePattern.php \Drupal\view_mode_page\Entity\ViewmodepagePattern::applies()

Determines if this pattern can apply a given object.

Parameters

\Drupal\Core\Entity\EntityInterface $entity: The entity used to determine if this plugin can apply.

Return value

bool Returns true or false

Overrides ViewmodepagePatternInterface::applies

File

src/Entity/ViewmodepagePattern.php, line 387

Class

ViewmodepagePattern
Defines the Viewmodepage pattern entity.

Namespace

Drupal\view_mode_page\Entity

Code

public function applies(EntityInterface $entity) {
  if ($this
    ->getAliasType()
    ->applies($entity)) {
    $definitions = $this
      ->getAliasType()
      ->getContextDefinitions();
    if (count($definitions) > 1) {
      throw new \Exception("Alias types do not support more than one context.");
    }
    $keys = array_keys($definitions);

    // Set the context object on our Alias plugin before retrieving contexts.
    $this
      ->getAliasType()
      ->setContextValue($keys[0], $entity);

    /** @var \Drupal\Core\Plugin\Context\ContextInterface[] $base_contexts */
    $contexts = $this
      ->getContexts();

    /** @var \Drupal\Core\Plugin\Context\ContextHandler $context_handler */
    $context_handler = \Drupal::service('context.handler');
    $conditions = $this
      ->getSelectionConditions();
    foreach ($conditions as $condition) {
      if ($condition instanceof ContextAwarePluginInterface) {
        try {
          $context_handler
            ->applyContextMapping($condition, $contexts);
        } catch (ContextException $e) {
          watchdog_exception('view_mode_page', $e);
          return FALSE;
        }
      }
      $result = $condition
        ->execute();
      if ($this
        ->getSelectionLogic() == 'and' && !$result) {
        return FALSE;
      }
      elseif ($this
        ->getSelectionLogic() == 'or' && $result) {
        return TRUE;
      }
    }
    return TRUE;
  }
  return FALSE;
}