-4

In the function hook_node_form_alter(), i still want to get an other node, because i want to set a field value programmatically in dependencies on the value of an other node (the parent node). But i still getting this error:

ParseError: syntax error, unexpected 'const' (T_CONST), expecting variable (T_VARIABLE)

My code looks like:

    use Drupal\Core\Form\FormStateInterface;
    use Drupal\Core\Entity\EntityInterface;
    use Drupal\Core\Ajax\CommandInterface;
    use Drupal\Core\Ajax\AjaxResponse;
    use Drupal\Core\Ajax\ReplaceCommand;
    use Drupal\Core\Entity;
    use Drupal\node\Entity\Node;


    function registry_codes_form_node_form_alter (&$form, FormStateInterface $form_state) {
      $node = $form_state->getFormObject()->getEntity();
      if($node->getType() === 'inspirecodes') {       // muss umgewandelt werden in in_array, sobald es eine Konfiguration gibt
        //wird ausgeführt nachdem Änderungen am Formular durchgeführt wurden
        if(isset($form_state->getValue('field_codes_parent')[0]['target_id']) || isset($form_state->getValue('title')[0]['value']) ) {
          $valueParent = null;
          $valueName = null;
          $tmp = null;
          if(isset($form_state->getValue('field_codes_parent')[0]['target_id'])) {
            dsm("huhu");
            $nid = $form_state->getValue('field_codes_parent')[0]['target_id']; 
            dsm($nid); //works
            $node_storage = \Drupal::entityManager()->getStorage('node');
            dd($node_storage); //does not work
            $parent = $node_storage->load($nid); //does also not work
            $valueParent = $parent->field_codes_code->value;
          }
          if ($form_state->getValue('title')[0]['value'] !== null) {
            $valueName = $form_state->getValue('title')[0]['value'];
          }

          if($valueParent !== null && $valueName !== null)  {
            $tmp = $valueParent . '.' . $valueName;
          } elseif ($valueParent !== null && $valueName === null){
            $tmp = $valueParent;
          } elseif($valueParent === null && $valueName !== null) {
            $tmp = $valueName;
          }
          $form['field_codes_code']['widget'][0]['value']['#default_value'] = $tmp;
        }
    return $form; 
    }
}

The problem seems to be the part with:

$node_storage = \Drupal::entityManager()->getStorage('node');

Clarification:

In the given hook function i want to get field information of an other node, but i can not get the node object. I always get the given error:

ParseError: syntax error, unexpected 'const' (T_CONST), expecting variable (T_VARIABLE)

If i delete the lines:

$node_storage = \Drupal::entityManager()->getStorage('node');
dd($node_storage); //does not work
$parent = $node_storage->load($nid); //does also not work

The function is still working, but i have not the needed information of the node.

This codeworks for me, but the function node_load() is deprecated:

if(isset($form_state->getValue('field_codes_parent')[0]['target_id'])) {
        $nid = $form_state->getValue('field_codes_parent')[0]['target_id']; 
        //$node_storage = \Drupal::entityTypeManager()->getStorage('node');
        //dd($node_storage);
        //$parent = $node_storage->load($nid);
        $parent = node_load(5);
        $valueParent = $parent->field_codes_code->value;
      }

closed as unclear what you're asking by leymannx, Pierre.Vriens, Kevin, No Sssweat, mradcliffe Jan 14 at 21:31

Please clarify your specific problem or add additional details to highlight exactly what you need. As it's currently written, it’s hard to tell exactly what you're asking. See the How to Ask page for help clarifying this question. If this question can be reworded to fit the rules in the help center, please edit the question.

  • You're missing a trailing right curly brace from your code block above. It was a bit difficult to parse as it didn't follow code standards. It helps to have a nice editor with phpcs and php running to both lint and syntax check your document. – mradcliffe Jan 14 at 21:30
-4
$node_storage = \Drupal::entityTypeManager()->getStorage('node');

and

return $form; // Semi column missing
  • These code works for me: – AndyLicht Jan 15 at 16:05

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