How to get node field values and include it the Rules Action? If I have a custom content type that has taxonomy terms, and I want to display these terms in Action. Example output is simply logging the terms.

Event: content type is created
Condition: of node type: custom_content
Action: Log the taxonomy terms

Pseudo code:

 public function execute() {
    $term1 = field_taxonomy1_fromCustomContentType
    $term2 = field_taxonomy2_fromCustomContentType
    $this->setProvidedValue('concatenated', $term1 . $term2);
    \Drupal::logger('custom_rules')->notice("Rules Action");
  }

Comments

johnreytanquinco created an issue. See original summary.

TR’s picture

Category: Task » Support request
Status: Active » Postponed (maintainer needs more info)

Are you asking for someone to write your code for you? You don't explain what issue you're having. Accessing field values on a node is a core Drupal function (not Rules-specific), and there's lots of core documentation on how to do that, if that's what you don't understand. Perhaps you should try to write the code for your action then post it here if it doesn't work. There are many examples in src/Plugin/RulesAction that you can use as a start. This seems to be a pretty straightforward task to me.

johnreytanquinco’s picture

Hi TR, thanks for your response. My goal is to get the taxonomy term name from the node. This is my simple Rules Action to hopefully display the field field_pattern_type, but getting error message.

<?php

namespace Drupal\pattern_rules\Plugin\RulesAction;

use Drupal\rules\Core\RulesActionBase;
use Drupal\Core\Entity\EntityInterface;

/**
 * Provides a test action that concatenates a string to itself.
 *
 * @RulesAction(
 *   id = "pattern_created",
 *   label = @Translation("Pattern Action"),
 *   category = @Translation("Custom")
 * )
 */

class PatternCreated extends RulesActionBase {

  /**
   * Access the Entity.
   *
   */

protected function doExecute(EntityInterface $entity) {
    $entity->get('field_pattern_type')->getValue();
    \Drupal::logger('pattern_rules')->notice("Logging Rules Action");
    \Drupal::logger('pattern_rules')->notice($entity);
  }
}

Error

The website encountered an unexpected error. Please try again later.
TypeError: Argument 1 passed to Drupal\pattern_rules\Plugin\RulesAction\PatternCreated::doExecute() must implement interface Drupal\Core\Entity\EntityInterface, none given in Drupal\pattern_rules\Plugin\RulesAction\PatternCreated->doExecute() (line 26 of modules/custom/spa/pattern_rules/src/Plugin/RulesAction/PatternCreated.php).
TR’s picture

Status: Postponed (maintainer needs more info) » Active

You need a context definition in your class annotation, in order to specify what arguments are needed to be passed into the action and what their datatypes are. Your annotation should look something like this:

 * @RulesAction(
 *   id = "pattern_created",
 *   label = @Translation("Pattern Action"),
 *   category = @Translation("Custom"),
 *   context = {
 *     "entity" = @ContextDefinition("entity",
 *       label = @Translation("Entity"),
 *       description = @Translation("Specifies the entity.")
 *     )
 *   }
 * )
johnreytanquinco’s picture

Thanks TR. That works as expected. Now, how can I output the field value, specifically I want to access taxonomy term of the node created.
I tried this for getting the value.

$result = $entity->get('field_pattern_type')->getValue();
\Drupal::logger('pattern_rules')->notice($result);

But the result in log is Array

BTW, for my ENTITY, I select node in the Data selector.

TR’s picture

Status: Active » Fixed
$terms = $entity->field_pattern_type->referencedEntities();
$result = [];
foreach ($terms as $term) {
  $result[] = $term->name->value;
}
\Drupal::logger('pattern_rules')->notice('Entity has terms: ' . implode(', ', $result));
johnreytanquinco’s picture

Wow! Thank you very much TR! I really appreciate your helping hand! :) Im new to php so im still trying my best to understand these functionalities.

johnreytanquinco’s picture

Status: Fixed » Closed (fixed)

Automatically closed - issue fixed for 2 weeks with no activity.