Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Context condition for Parent node of node has term #1053

Merged
merged 3 commits into from
Sep 27, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
75 changes: 75 additions & 0 deletions src/Plugin/Condition/ParentNodeOfNodeHasTerm.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
<?php

namespace Drupal\islandora\Plugin\Condition;

use Drupal\Core\Form\FormStateInterface;

/**
* 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 buildConfigurationForm(array $form, FormStateInterface $form_state) {
$form = parent::buildConfigurationForm($form, $form_state);
// Make term not required.
$form['term']['#required'] = FALSE;
return $form;
}

/**
* {@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']]);
}
}

}