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
}
}
}