Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save modcab/9e3fc2616754313f4a0e9914ac45427c to your computer and use it in GitHub Desktop.
Save modcab/9e3fc2616754313f4a0e9914ac45427c to your computer and use it in GitHub Desktop.
Drupal 8 - Content entities - How __get (magical method) vs get (explicit method) work
<?php
public function get($field_name) {
if (!isset($this->fields[$field_name][$this->activeLangcode])) {
return $this->getTranslatedField($field_name, $this->activeLangcode);
}
return $this->fields[$field_name][$this->activeLangcode];
}
public function &__get($name) {
// If this is an entity field, handle it accordingly. We first check whether
// a field object has been already created. If not, we create one.
if (isset($this->fields[$name][$this->activeLangcode])) {
return $this->fields[$name][$this->activeLangcode];
}
// Inline getFieldDefinition() to speed things up.
if (!isset($this->fieldDefinitions)) {
$this->getFieldDefinitions();
}
if (isset($this->fieldDefinitions[$name])) {
$return = $this->getTranslatedField($name, $this->activeLangcode);
return $return;
}
// Else directly read/write plain values. That way, non-field entity
// properties can always be accessed directly.
if (!isset($this->values[$name])) {
$this->values[$name] = NULL;
}
return $this->values[$name];
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment