0

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.

0

1 Answer 1

0

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();

Your Answer

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

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