diff --git a/core/lib/Drupal/Core/Entity/ContentEntityBase.php b/core/lib/Drupal/Core/Entity/ContentEntityBase.php index 3e94b7d..406efa6 100644 --- a/core/lib/Drupal/Core/Entity/ContentEntityBase.php +++ b/core/lib/Drupal/Core/Entity/ContentEntityBase.php @@ -541,6 +541,51 @@ protected function getTranslatedField($name, $langcode) { } /** + * Gets the value of a specific property of a field. + * + * Only the first delta can be accessed with this method. + * + * @param string $field_name + * The name of the field. + * @param string $property + * The field property, "value" for many field types. + * + * @return mixed + */ + public function getFieldValue($field_name, $property) { + // Attempt to get the value from the values directly if the field is not + // initialized yet. + if (!isset($this->fields[$field_name])) { + $field_values = NULL; + if (isset($this->values[$field_name][$this->activeLangcode])) { + $field_values = $this->values[$field_name][$this->activeLangcode]; + } + elseif ($this->values[$field_name][LanguageInterface::LANGCODE_DEFAULT]) { + $field_values = $this->values[$field_name][LanguageInterface::LANGCODE_DEFAULT]; + } + + if ($field_values !== NULL) { + // If there are field values, try to get the property value. + // Configurable/Multi-value fields are stored differently, try accessing + // with delta and property first, then without delta and last, if the + // value is a scalar, just return that. + if (isset($field_values[0][$property]) && is_array($field_values[0])) { + return $field_values[0][$property]; + } + elseif (isset($field_values[$property]) && is_array($field_values)) { + return $field_values[$property]; + } + elseif (!is_array($field_values)) { + return $field_values; + } + } + } + + // Fall back to access the property through the field object. + return $this->get($field_name)->$property; + } + + /** * {@inheritdoc} */ public function set($name, $value, $notify = TRUE) { diff --git a/core/modules/comment/src/CommentLazyBuilders.php b/core/modules/comment/src/CommentLazyBuilders.php index 730c840..b49a533 100644 --- a/core/modules/comment/src/CommentLazyBuilders.php +++ b/core/modules/comment/src/CommentLazyBuilders.php @@ -163,7 +163,7 @@ public function renderLinks($comment_entity_id, $view_mode, $langcode, $is_in_pr */ protected function buildLinks(CommentInterface $entity, EntityInterface $commented_entity) { $links = []; - $status = $commented_entity->get($entity->getFieldName())->status; + $status = $commented_entity->getFieldValue($entity->getFieldName(), 'status'); if ($status == CommentItemInterface::OPEN) { if ($entity->access('delete')) { diff --git a/core/modules/comment/src/Entity/Comment.php b/core/modules/comment/src/Entity/Comment.php index a5f1966..f68d42e 100644 --- a/core/modules/comment/src/Entity/Comment.php +++ b/core/modules/comment/src/Entity/Comment.php @@ -351,21 +351,25 @@ public function getParentComment() { * {@inheritdoc} */ public function getCommentedEntity() { - return $this->get('entity_id')->entity; + if ($this->getCommentedEntityTypeId() && $entity_id = $this->getCommentedEntityId()) { + return $this->entityTypeManager() + ->getStorage($this->getCommentedEntityTypeId()) + ->load($entity_id); + } } /** * {@inheritdoc} */ public function getCommentedEntityId() { - return $this->get('entity_id')->target_id; + return $this->getFieldValue('entity_id', 'target_id'); } /** * {@inheritdoc} */ public function getCommentedEntityTypeId() { - return $this->get('entity_type')->value; + return $this->getFieldValue('entity_type', 'value'); } /** @@ -380,7 +384,7 @@ public function setFieldName($field_name) { * {@inheritdoc} */ public function getFieldName() { - return $this->get('field_name')->value; + return $this->getFieldValue('field_name', 'value'); } /** @@ -529,7 +533,7 @@ public function getOwner() { * {@inheritdoc} */ public function getOwnerId() { - return $this->get('uid')->target_id; + return $this->getFieldValue('uid', 'target_id'); } /**