forked from alxp/islandora
-
Notifications
You must be signed in to change notification settings - Fork 118
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
#2348 Define ParentNodeOfNodeHasTerm context condition
- Loading branch information
Jonathan Hunt
committed
Aug 29, 2024
1 parent
b0df472
commit bfea29b
Showing
1 changed file
with
70 additions
and
0 deletions.
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
<?php | ||
|
||
namespace Drupal\islandora\Plugin\Condition; | ||
|
||
use Drupal\Core\Condition\ConditionPluginBase; | ||
use Drupal\Core\Entity\EntityInterface; | ||
use Drupal\Core\Entity\EntityTypeManagerInterface; | ||
use Drupal\Core\Form\FormStateInterface; | ||
use Drupal\Core\Plugin\ContainerFactoryPluginInterface; | ||
use Symfony\Component\DependencyInjection\ContainerInterface; | ||
|
||
/** | ||
* Provides a 'term' condition for a node's parent. | ||
* | ||
* Like parent_node_has_term but applicable to a node instead of media. | ||
* Like node_has_term but applicable to parent node. | ||
* A typical use case is to test for a child node within a compound item. | ||
* | ||
* @Condition( | ||
* id = "parent_node_of_node_has_term", | ||
* label = @Translation("Parent node for node has term with URI"), | ||
* context_definitions = { | ||
* "node" = @ContextDefinition("entity:node", required = TRUE , label = @Translation("node")) | ||
* } | ||
* ) | ||
*/ | ||
class ParentNodeOfNodeHasTerm extends NodeHasTerm { | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function evaluate() { | ||
if (empty($this->configuration['uri']) && !$this->isNegated()) { | ||
return TRUE; | ||
} | ||
|
||
$node = $this->getContextValue('node'); | ||
if (!$node) { | ||
return FALSE; | ||
} | ||
|
||
$fields = [$this->utils::MEMBER_OF_FIELD]; | ||
$parentIds = $this->utils->findAncestors($node, $fields, 1); | ||
if (!$parentIds) { | ||
return FALSE; | ||
} | ||
$result = 0; | ||
foreach ($parentIds as $parentId) { | ||
$parent = $this->entityTypeManager->getStorage('node')->load($parentId); | ||
if ($parent) { | ||
$parentResult = $this->evaluateEntity($parent); | ||
$result = $result + (int)$parentResult; | ||
} | ||
} | ||
return (boolean)$result; | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function summary() { | ||
if (!empty($this->configuration['negate'])) { | ||
return $this->t('The parent node is not associated with taxonomy term with uri @uri.', ['@uri' => $this->configuration['uri']]); | ||
} | ||
else { | ||
return $this->t('The parent node is associated with taxonomy term with uri @uri.', ['@uri' => $this->configuration['uri']]); | ||
} | ||
} | ||
|
||
} |