Skip to main content
added 25 characters in body
Source Link
avpaderno
  • 97.9k
  • 15
  • 164
  • 284

For nodes, like for every content entity, you can use the following code similar to the following one to check if thea Field API field(*) exists.

if ($node->hasField('field_version')) {
  // The node has that field; access it with $node->hasField('field_version').
}

A more generic code that would work for either Field API fields or non-field properties would be similar to the following one.

if (isset($node->field_version)) {
  // The node has that field/property; access it with $node->field_version.
}

* I am using the terminology used in a comment for code in ContentEntityBase::__isset().

// "Official" Field API fields are always set. For non-field properties,
// check the internal values.
return $this->hasField($name) ? TRUE : isset($this->values[$name]);

For nodes, like for every content entity, you can use the following code to check if the Field API field(*) exists.

if ($node->hasField('field_version')) {
  // The node has that field; access it with $node->hasField('field_version').
}

A more generic code that would work for either Field API fields or non-field properties would be the following one.

if (isset($node->field_version)) {
  // The node has that field/property; access it with $node->field_version.
}

* I am using the terminology used in a comment for code in ContentEntityBase::__isset().

// "Official" Field API fields are always set. For non-field properties,
// check the internal values.
return $this->hasField($name) ? TRUE : isset($this->values[$name]);

For nodes, like for every content entity, you can use code similar to the following one to check if a Field API field(*) exists.

if ($node->hasField('field_version')) {
  // The node has that field; access it with $node->hasField('field_version').
}

A more generic code that would work for either Field API fields or non-field properties would be similar to the following one.

if (isset($node->field_version)) {
  // The node has that field/property; access it with $node->field_version.
}

* I am using the terminology used in a comment for code in ContentEntityBase::__isset().

// "Official" Field API fields are always set. For non-field properties,
// check the internal values.
return $this->hasField($name) ? TRUE : isset($this->values[$name]);
Source Link
avpaderno
  • 97.9k
  • 15
  • 164
  • 284

For nodes, like for every content entity, you can use the following code to check if the Field API field(*) exists.

if ($node->hasField('field_version')) {
  // The node has that field; access it with $node->hasField('field_version').
}

A more generic code that would work for either Field API fields or non-field properties would be the following one.

if (isset($node->field_version)) {
  // The node has that field/property; access it with $node->field_version.
}

* I am using the terminology used in a comment for code in ContentEntityBase::__isset().

// "Official" Field API fields are always set. For non-field properties,
// check the internal values.
return $this->hasField($name) ? TRUE : isset($this->values[$name]);