1

I'm trying to retrieve a value from a Node object. It looks like the value is there, when debugging and checking out the object. But somehow it cannot be accessed.

Trying to get the field:

// $this->entityManager is an instance of EntityTypeManagerInterface 
$node = $this->entityManager->getStorage('node')->load($nodeId);

// hasField always returns FALSE
$node->hasField('field_google_hire_identifier');
// $node->get('field_google_hire_identifier'); returns unknown field error

Debugging $node->hasField, looking inside getFieldDefinition:

  public function getFieldDefinition($name) {
    if (!isset($this->fieldDefinitions)) {
      $this->getFieldDefinitions();
    }
    if (isset($this->fieldDefinitions[$name])) {
      // This code should be executed, but it is not.
      return $this->fieldDefinitions[$name];
    }
  }

Debugging values

The second if statement content is not executed, with my understanding it should be.

The node was created with migrate.

I disabled APC and Opcache.

4
  • I ran into something similar with migrate - if the field was on 2 node types, then field definition was returning null no matter what. And I was able to assert this in a kernel test I was making for organic group migration but I have not been able to reproduce it in a test in core only. I think perhaps there are some missing configuration during some migrations, but that's as far as I have gotten. I don't think this is on-topic and is probably related to whichever module provided those fields. – mradcliffe Feb 6 '19 at 21:29
  • Alright, I'll probably dig inside the database to find out more. I created the field (plain text) with the UI. But it looks fine when I step through the code with the debugger. All the data is there (on the node object). Strangely I can't get it. – Orlando Feb 7 '19 at 7:45
  • Looking forward to it. If you can reproduce it in core somewhere please file an issue and ping me here. – mradcliffe Feb 7 '19 at 20:51
  • It looks like your issue was unrelated, @Orlando, but I realized I did file a core issue related to field definitions being null (not your issue), and did some work on it last year. My memory is just bad - drupal.org/project/drupal/issues/3008202. Adding this for posterity and my own memory. – mradcliffe Feb 25 '19 at 12:52
1

The field name string contained a right to left mark. See https://stackoverflow.com/questions/54653637/cannot-get-array-key-array-key-not-set/54655517#54655517 to find out how to remove it

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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