Score:1

How can I disable revisioning on specific node field

de flag

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?

sonfd avatar
in flag
How is it tracking the changes exactly? Is it just a display field? or is it actually storing a value in the database?
leymannx avatar
ne flag
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.
Score:0
de flag

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');
  }
}
mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.