1

I currently use hook_entity_extra_field_info() and hook_ENTITY_TYPE_view() to create a custom field displayed in nodes that generates content dynamically via PHP.

However, Drupal 9 seems to be tracking changes to the output of this field each time I save a node revision, which I don't want because that field's content is generated dynamically and is already under version control in a custom module.

Is there a way to tell Drupal in one of those hooks, or anywhere else in the configuration, not to track revisions for a specific node field?

2
  • 1
    How is it tracking the changes exactly? Is it just a display field? or is it actually storing a value in the database?
    – sonfd
    May 2, 2022 at 19:56
  • What does generates content dynamically mean exactly? Can you show us some sample code? If you generate content in the sense of attaching something to the same node of course revisioning kicks in if you don't store the stuff somewhere else. Pseudo fields are normally only used for displaying computed markup, not for generating content.
    – leymannx
    May 3, 2022 at 5:52

1 Answer 1

0

As the content of your field is dynamically generated, it sounds like you need a computed field. Which will remove the issue you are running into. A computed field is one that is generated dynamically, rather than through user input. As it is a field, it can be managed like any other field in Drupal (though integration with Views requires some additional efforts). Fields are also cached with Drupal's various cache APIs.

To create a computed field, first extend Drupal\Core\Field\FieldItemList, use the Drupal\Core\TypedData\ComputedItemListTrait, and implement the computeValue() method:

namespace Drupal\[EXAMPLE]\Plugin\Field;

use Drupal\Core\Field\FieldItemList;
use Drupal\Core\TypedData\ComputedItemListTrait;

class SomeDynamicField extends FieldItemList {

  use ComputedItemListTrait;      

  /**
   * {@inheritdoc}
   */
  protected function computeValue() {
    $values = some_function_to_get_an_array_of_values();
    foreach ($values as $index => $value) {
      $this->list[$delta] = $this->createItem($delta, $value);
    }
  }
}

Next, this field needs to be added to each entity type that you need, in hook_entity_base_field_info_alter():

use Drupal\Core\Entity\EntityTypeInterface;
use Drupal\Core\Field\BaseFieldDefinition;

/**
 * Implements hook_entity_base_field_info_alter().
 */
function EXAMPLE_entity_base_field_info_alter(&$fields, EntityTypeInterface $entity_type) {
  // Add/alter entity types as necessary.
  $applicable_entity_types = ['node'];
  if (in_array($entity_type->id(), $applicable_entity_types)) {
    $fields['my_computed_field'] = BaseFieldDefinition::create('string')
      ->setName('example_field')
      ->setLabel(t('Example Calculated Field'))
      ->setDescription(t('An example of a calculated field'))
      // Set the field as a computed field.
      ->setComputed(TRUE)
      // Set the class that generates the field value(s).
      ->setClass('\Drupal\[EXAMPLE]\Plugin\Field\Computed\SomeDynamicField');
  }
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.