0

I've created a Custom Field (let's call it 'Reference') whose value requires the Entity Id.

To accomplish that I modify the Field value in the postSave method, like this:

if (!$update) {
  $this->get('value')
    ->setValue($this->getCalculatedReferenciaValue());
}

public function getCalculatedReferenciaValue() {
  //...
  $entity = $this->getEntity();
  $id = $entity->id();
  $year = date("Y", $date);
  // default format: 'SWAN%s%08dIMOV'
  $format = $this->getSetting('format');
  return sprintf($format, $year, $id);
}

Then, in pathauto, I use that custom created field, as a token, to generate the path.

However, for some reason, when creating a node, my custom field still has the default value.

If I then edit and save the node, the custom field (and the auto path) is updated correctly.

Q: Is there a way to update the field value so that it becomes available when generating the autopath for the first time?

1 Answer 1

0

For getting the node ID you need to first save the node. For eg:

$node = Node::create([
      'type' => 'my_content_type',
      'title' => 'My Title',
      'uid' => \Drupal::currentUser()->id(),
      'status' => 1
]);
$node->save();
    :
    :

$id = $node->id();
$year = date("Y", $date);
// default format: 'SWAN%s%08dIMOV'
$format = $this->getSetting('format');
$node->field_custom->value = sprintf($format, $year, $id);
$node->save();

Your Answer

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

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