Skip to content

Instantly share code, notes, and snippets.

@chrisfromredfin
Created November 18, 2020 19:47
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save chrisfromredfin/d5b583c30129ac6f98d9687768eb2734 to your computer and use it in GitHub Desktop.
Save chrisfromredfin/d5b583c30129ac6f98d9687768eb2734 to your computer and use it in GitHub Desktop.
A "not node type" condition plugin
<?php
namespace Drupal\YOURMODULE\Plugin\Condition;
use Drupal\node\Plugin\Condition\NodeType;
use Drupal\Core\Plugin\ContainerFactoryPluginInterface;
use Drupal\node\NodeInterface;
/**
* Provides a 'Not Node Type' condition.
*
* @Condition(
* id = "not_node_type",
* label = @Translation("Not Node Bundle"),
* )
*/
class NotNodeType extends NodeType implements ContainerFactoryPluginInterface {
/**
* {@inheritdoc}
*/
public function summary() {
if (count($this->configuration['bundles']) > 1) {
$bundles = $this->configuration['bundles'];
$last = array_pop($bundles);
$bundles = implode(', ', $bundles);
return $this->t('The node bundle is not @bundles or @last', [
'@bundles' => $bundles,
'@last' => $last,
]);
}
$bundle = reset($this->configuration['bundles']);
return $this->t('The node bundle is not @bundle', [
'@bundle' => $bundle,
]);
}
/**
* {@inheritdoc}
*/
public function evaluate() {
if (empty($this->configuration['bundles']) && !$this->isNegated()) {
return TRUE;
}
$node = \Drupal::routeMatch()->getParameter('node');
if ($node instanceof NodeInterface) {
$bundle = $node->bundle();
if (in_array($bundle, $this->configuration['bundles'])) {
return FALSE;
}
else {
return TRUE;
}
}
else {
return TRUE;
}
return !empty($this->configuration['bundles'][$node->getType()]);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment