Created
November 18, 2020 19:47
-
-
Save chrisfromredfin/d5b583c30129ac6f98d9687768eb2734 to your computer and use it in GitHub Desktop.
A "not node type" condition plugin
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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