0

I'm trying to write a small module that copies the body content from one node to another node. The module does almost what it should and everything arrives so far. However, I have to open the node to be copied once, then save it and then open it again. Then it has taken over everything correctly. But I would like to have the field already copied when I open the node. Since this is my beginnings in module development, I would of course appreciate any tips. I am also sure that this can be done more elegantly.

/**
 * Implements hook_form_alter().
 */
function copy_fields_from_model_contract_form_alter(&$form, FormStateInterface $form_state, $form_id) {
  
  $nid = 6766;
  $node_storage = \Drupal::entityTypeManager()->getStorage('node');
  $node = $node_storage->load($nid);
  if ($nid == '6766') {
    $getcontract = $node->field_vertragsinhalt->value;
  }

  $nid = 6716;
  $node_storage = \Drupal::entityTypeManager()->getStorage('node');
  $node = $node_storage->load($nid);
  if ($nid == '6716') {
    if ($form_id == 'node_buch_anlegen_edit_form') {
      $node->field_vertragsinhalt->value = $getcontract;
      $node->body->format = 'full_html';
      $node->save();
      dpm("Testsetest");
    }
  }
}

2 Answers 2

1

Use hook_ENTITY_TYPE_prepare_form if you want to change a field value before the form is built for an existing node:

<?php

use Drupal\node\NodeInterface;
use Drupal\Core\Form\FormStateInterface;


/**
 * Implements hook_ENTITY_TYPE_prepare_form() for node entities.
 */
function mymodule_node_prepare_form(NodeInterface $node, $operation, FormStateInterface $form_state) {
  if ($operation == 'edit') {
    if ($node->id() == '6716') {
      $node->field_vertragsinhalt->value = $getcontract;
      $node->body->format = 'full_html';
      // don't save the node yet, it will be saved after submitting
    }
  }
}

For the more common case of setting field values when creating new nodes see Pre-populate an entity reference field (media image) when creating a node

0

Vielen dank dafür, dass war genau das was ich brauchte. Und ich habe das auch soweit hin bekommen in der node edit version:

<?php

use Drupal\node\NodeInterface;
use Drupal\Core\Form\FormStateInterface;


/**
 * Implements hook_ENTITY_TYPE_prepare_form() for node entities.
 */
function copy_fields_from_model_contract_node_edit_node_prepare_form(NodeInterface $node, $operation, FormStateInterface $form_state)
{

//TODO Build NID as array and go through with loop

  $nid = 6766;
  $node_storage = \Drupal::entityTypeManager()->getStorage('node');
  $node = $node_storage->load($nid);

  if ($nid == '6766') {
    $getcontract = $node->field_vertragsinhalt->value;
  }

  //TODO Works without ($operation == 'default')

  if ($operation == 'edit')  {
    dpm ("Modul an edit");
    //TODO Shall always pull the current node and nid. But does not work yet with node creation. Error: Call to a member function id() on null in

    $node = \Drupal::routeMatch()->getParameter('node');
    $nid = $node->id();
    $node_storage = \Drupal::entityTypeManager()->getStorage('node');
    $node = $node_storage->load($nid);

    if ($node->getType() == 'buch_anlegen') {
      dpm($nid);
      $node->field_vertragsinhalt->value = $getcontract;
      $node->body->format = 'full_html';
      // don't save the node yet, it will be saved after submitting
    }
  }
}

That also does what it is supposed to. Now I would like to write the content when the node is created. There the hook also geift so far.

I have edited for this:

 if ($operation == 'edit')

to

 if (($operation == 'edit')|| ($operation == 'default'))

But then it throws me an error in line 30:

$nid = $node->id();

Could it be that when a node is created at this point the ID is not yet assigned? Or do I have a knot in my head now somehow. I'm trying to use hook_entity_type_create for this geread. The example for this:

/**
 * Implements hook_ENTITY_TYPE_create() for 'node'.
 */
function mymodule_node_create(\Drupal\node\NodeInterface $node) {

  if ($node->getType() == 'my_content_type') {
    $media = \Drupal::entityTypeManager()->getStorage('media')->load(38);
    $node->field_image[] = $media;
  }

}

Or am I all wrong there? And how do I get this to fit my case? This is what I have so far:

<?php

use Drupal\node\NodeInterface;
use Drupal\Core\Form\FormStateInterface;


/**
 * Implements hook_ENTITY_TYPE_prepare_form() for node entities.
 */
function copy_fields_from_model_contract_node_create_node_create(NodeInterface $node)
{
//TODO Build NID as array and go through with loop

  $nid = 6766;
  $node_storage = \Drupal::entityTypeManager()->getStorage('node');
  $node = $node_storage->load($nid);

  if ($nid == '6766') {
    $getcontract = $node->field_vertragsinhalt->value;
  }

  //TODO Works without ($operation == 'default')

  if ($node->getType() == 'mustervertrage') {
    /*dpm ($node->field_vertragsinhalt->value);*/
    //TODO Shall always pull the current node and nid. But does not work yet with node creation. Error: Call to a member function id() on null in

//        $node = \Drupal::routeMatch()->getParameter('node');
//        $nid = $node->id();
    $node_storage = \Drupal::entityTypeManager()->getStorage('node');
    $node = $node_storage->load($nid);

    if ($node->getType() == 'mustervertrage') {
      dpm($nid);
      $node->field_vertragsinhalt->value = $getcontract;
      $node->body->format = 'full_html';
      // don't save the node yet, it will be saved after submitting
    }
  }
}

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.