You are here

public function PathautoPattern::applies in Pathauto 8

Determines if this pattern can apply a given object.

Parameters

$object: The object used to determine if this plugin can apply.

Return value

bool

Overrides PathautoPatternInterface::applies

File

src/Entity/PathautoPattern.php, line 336

Class

PathautoPattern
Defines the Pathauto pattern entity.

Namespace

Drupal\pathauto\Entity

Code

public function applies($object) {
  if ($this
    ->getAliasType()
    ->applies($object)) {
    $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], $object);

    /** @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) {

      // As the context object is kept and only the value is switched out,
      // it can over time grow to a huge number of cache contexts. Reset it
      // if there are 100 cache tags to prevent cache tag merging getting too
      // slow.
      foreach ($condition
        ->getContextDefinitions() as $name => $context_definition) {
        if (count($condition
          ->getContext($name)
          ->getCacheTags()) > 100) {
          $condition
            ->setContext($name, new Context($context_definition));
        }
      }
      if ($condition instanceof ContextAwarePluginInterface) {
        try {
          $context_handler
            ->applyContextMapping($condition, $contexts);
        } catch (ContextException $e) {
          watchdog_exception('pathauto', $e);
          return FALSE;
        }
      }
      $result = $condition
        ->execute();
      if ($this
        ->getSelectionLogic() == 'and' && !$result) {
        return FALSE;
      }
      elseif ($this
        ->getSelectionLogic() == 'or' && $result) {
        return TRUE;
      }
    }
    return TRUE;
  }
  return FALSE;
}