Created
May 19, 2017 10:16
-
-
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
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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