Score:0

Unable to update node entity fields using custom form

zm flag

There is a certain content type, in which there is a field to enter a node or taxonomy. I want to create a new node for that content type using a custom form and register the node or taxonomy in the entity field.

$form['client_company'] = [
  '#type' => 'entity_autocomplete',
  '#target_type' => 'taxonomy_term',
  '#tags' => TRUE,
  '#selection_settings' => array(
    'target_bundles' => array('vid', 'client'),
  ),
  '#weight' => '0',
];

$form['client_human'] = [
  '#type' => 'entity_autocomplete',
  '#target_type' => 'node',
  '#tags' => TRUE,
  '#selection_settings' => array(
    'target_bundles' => array('budle', 'client_human'),
  ),
  '#weight' => '0',
];
$form['url'] = [
  '#type' => 'url',
];


$node->field_project_refercompany->value = $form_state->getValue('client_company');        
$node->field_project_referclienthuman->value = $form_state->getValue('client_human');
$node->field_project_url->value = $form_state->getValue('url');
$node->save();

The $node, field_refercompany, field_project_referclienthuman, etc. are nodes and their field names, no error there. However, even though I can create a new node, the above fields are blank and nothing is entered.

Score:0
zm flag

Self Answer

$node->field_project_refercompany->target_id = $form_state->getValue('client_company')[0]['target_id'];
$node->field_project_referclienthuman->target_id = $form_state->getValue('client_human')[0]['target_id'];
$node->field_project_url->uri = Url::fromUri($form_state->getValue('url'))->toString();
I sit in a Tesla and translated this thread with Ai:

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.