diff --git a/core/lib/Drupal/Core/Entity/ContentEntityBase.php b/core/lib/Drupal/Core/Entity/ContentEntityBase.php index 53384b957188eb0100bfc0979fb4702dd02ff449..62e764b03d0129ad4c202342c8a06c4613aa8c83 100644 --- a/core/lib/Drupal/Core/Entity/ContentEntityBase.php +++ b/core/lib/Drupal/Core/Entity/ContentEntityBase.php @@ -363,6 +363,13 @@ public function uuid() { return $this->get('uuid')->value; } + /** + * {@inheritdoc} + */ + public function hasField($field_name) { + return (bool) $this->getPropertyDefinition($field_name); + } + /** * {@inheritdoc} */ @@ -817,7 +824,7 @@ public function __set($name, $value) { if (isset($this->fields[$name][$this->activeLangcode])) { $this->fields[$name][$this->activeLangcode]->setValue($value); } - elseif ($this->getPropertyDefinition($name)) { + elseif ($this->hasField($name)) { $this->getTranslatedField($name, $this->activeLangcode)->setValue($value); } // The translations array is unset when cloning the entity object, we just @@ -836,7 +843,7 @@ public function __set($name, $value) { * Implements the magic method for isset(). */ public function __isset($name) { - if ($this->getPropertyDefinition($name)) { + if ($this->hasField($name)) { return $this->get($name)->getValue() !== NULL; } else { @@ -848,7 +855,7 @@ public function __isset($name) { * Implements the magic method for unset. */ public function __unset($name) { - if ($this->getPropertyDefinition($name)) { + if ($this->hasField($name)) { $this->get($name)->setValue(NULL); } else { diff --git a/core/lib/Drupal/Core/Entity/ContentEntityInterface.php b/core/lib/Drupal/Core/Entity/ContentEntityInterface.php index 93f28b886f28bb5493ffc4e846678e207a64fd0b..ca4be508c1a7d830fac85721e9f178dcd65c2f63 100644 --- a/core/lib/Drupal/Core/Entity/ContentEntityInterface.php +++ b/core/lib/Drupal/Core/Entity/ContentEntityInterface.php @@ -55,4 +55,15 @@ public function initTranslation($langcode); */ public static function baseFieldDefinitions($entity_type); + /** + * Returns whether the entity has a field with the given name. + * + * @param string $field_name + * The field name. + * + * @return bool + * TRUE if the entity has a field with the given name. FALSE otherwise. + */ + public function hasField($field_name); + } diff --git a/core/lib/Drupal/Core/Entity/EntityRenderController.php b/core/lib/Drupal/Core/Entity/EntityRenderController.php index fe6bd103c967e42cdd94e3250ba5cfe40990e9e7..9d68684afe601826cddb8e2455dcae836fb38e65 100644 --- a/core/lib/Drupal/Core/Entity/EntityRenderController.php +++ b/core/lib/Drupal/Core/Entity/EntityRenderController.php @@ -70,7 +70,7 @@ public function buildContent(array $entities, array $displays, $view_mode, $lang // avoid the cost of calling $entity->getProperties() by iterating the // intersection as follows. foreach ($displays[$entity->bundle()]->getComponents() as $name => $options) { - if ($entity->getPropertyDefinition($name)) { + if ($entity->hasField($name)) { foreach ($entity->get($name) as $item) { $item->_attributes = array(); } diff --git a/core/modules/comment/comment.module b/core/modules/comment/comment.module index de83ead2f1389b8e4a2f80f6d44b2e40f92343a1..b31497f4decf71bc5c61cb0117dc5e211eae274e 100644 --- a/core/modules/comment/comment.module +++ b/core/modules/comment/comment.module @@ -478,7 +478,7 @@ function comment_entity_view(EntityInterface $entity, EntityDisplay $display, $v $fields = \Drupal::service('comment.manager')->getFields('node'); foreach ($fields as $field_name => $detail) { // Skip fields that entity does not have. - if (!$entity->getPropertyDefinition($field_name)) { + if (!$entity->hasField($field_name)) { continue; } $links = array(); @@ -1022,12 +1022,12 @@ function comment_entity_insert(EntityInterface $entity) { )); foreach ($fields as $field_name => $detail) { // Skip fields that entity does not have. - if (!$entity->getPropertyDefinition($field_name)) { + if (!$entity->hasField($field_name)) { continue; } // There is at least one comment field, the query needs to be executed. // @todo Use $entity->getAuthorId() after https://drupal.org/node/2078387 - if ($entity->getPropertyDefinition('uid')) { + if ($entity->hasField('uid')) { $last_comment_uid = $entity->get('uid')->value; } else { @@ -1105,7 +1105,7 @@ function comment_node_update_index(EntityInterface $node, $langcode) { if ($index_comments) { foreach (\Drupal::service('comment.manager')->getFields('node') as $field_name => $info) { // Skip fields that entity does not have. - if (!$node->getPropertyDefinition($field_name)) { + if (!$node->hasField($field_name)) { continue; } $instance = \Drupal::service('field.info')->getInstance('node', $node->getType(), $field_name); @@ -1142,7 +1142,7 @@ function comment_node_search_result(EntityInterface $node) { $open = FALSE; foreach ($comment_fields as $field_name => $info) { // Skip fields that entity does not have. - if (!$node->getPropertyDefinition($field_name)) { + if (!$node->hasField($field_name)) { continue; } // Do not make a string if comments are hidden. @@ -1570,7 +1570,7 @@ function template_preprocess_comment(&$variables) { } else { // @todo Use $entity->getAuthorId() after https://drupal.org/node/2078387 - if ($commented_entity->getPropertyDefinition('uid') && $comment->uid->target_id == $commented_entity->get('uid')->value) { + if ($commented_entity->hasField('uid') && $comment->uid->target_id == $commented_entity->get('uid')->value) { $variables['attributes']['class'][] = 'by-' . $commented_entity->entityType() . '-author'; } } diff --git a/core/modules/comment/lib/Drupal/comment/CommentStorageController.php b/core/modules/comment/lib/Drupal/comment/CommentStorageController.php index 2c8f100c8a026698b4656950227b300c2f634191..751549f446da951ec905082f36435314a0047ef5 100644 --- a/core/modules/comment/lib/Drupal/comment/CommentStorageController.php +++ b/core/modules/comment/lib/Drupal/comment/CommentStorageController.php @@ -108,7 +108,7 @@ public function updateEntityStatistics(CommentInterface $comment) { // @todo Use $entity->getAuthorId() after https://drupal.org/node/2078387 // Get the user ID from the entity if it's set, or default to the // currently logged in user. - 'last_comment_uid' => $entity->getPropertyDefinition('uid') ? $entity->get('uid')->value : \Drupal::currentUser()->id(), + 'last_comment_uid' => $entity->hasField('uid') ? $entity->get('uid')->value : \Drupal::currentUser()->id(), )) ->condition('entity_id', $comment->entity_id->value) ->condition('entity_type', $comment->entity_type->value) diff --git a/core/modules/path/path.module b/core/modules/path/path.module index c7d6b2697349907417fd8b31f1abd6f692d6cf1a..08d82d53c59ce31ca3c8e4c1a40536588b91240d 100644 --- a/core/modules/path/path.module +++ b/core/modules/path/path.module @@ -232,7 +232,7 @@ function path_entity_field_info($entity_type) { * @todo: Move this to methods on the FieldItem class. */ function path_entity_insert(EntityInterface $entity) { - if ($entity instanceof ContentEntityInterface && $entity->getPropertyDefinition('path')) { + if ($entity instanceof ContentEntityInterface && $entity->hasField('path')) { $entity->path->alias = trim($entity->path->alias); // Only save a non-empty alias. if (!empty($entity->path->alias)) { @@ -248,7 +248,7 @@ function path_entity_insert(EntityInterface $entity) { * Implements hook_entity_update(). */ function path_entity_update(EntityInterface $entity) { - if ($entity instanceof ContentEntityInterface && $entity->getPropertyDefinition('path')) { + if ($entity instanceof ContentEntityInterface && $entity->hasField('path')) { $entity->path->alias = trim($entity->path->alias); // Delete old alias if user erased it. if ($entity->path->pid && !$entity->path->alias) { @@ -269,7 +269,7 @@ function path_entity_update(EntityInterface $entity) { * Implements hook_entity_predelete(). */ function path_entity_predelete(EntityInterface $entity) { - if ($entity instanceof ContentEntityInterface && $entity->getPropertyDefinition('path')) { + if ($entity instanceof ContentEntityInterface && $entity->hasField('path')) { // Delete all aliases associated with this term. $uri = $entity->uri(); \Drupal::service('path.crud')->delete(array('source' => $uri['path'])); diff --git a/core/modules/system/tests/modules/entity_test/entity_test.module b/core/modules/system/tests/modules/entity_test/entity_test.module index e949e5035366929a5d03186e90265b7963c36588..d14d19b0313d046de15e24be62f55b272f665999 100644 --- a/core/modules/system/tests/modules/entity_test/entity_test.module +++ b/core/modules/system/tests/modules/entity_test/entity_test.module @@ -554,7 +554,7 @@ function entity_test_entity_prepare_view($entity_type, array $entities, array $d // Add a dummy field item attribute on field_test_text if it exists. if ($entity_type == 'entity_test_render') { foreach ($entities as $entity) { - if ($entity->getPropertyDefinition('field_test_text')) { + if ($entity->hasField('field_test_text')) { foreach ($entity->get('field_test_text') as $item) { $item->_attributes += array('data-field-item-attr' => 'foobar'); } diff --git a/core/modules/user/user.module b/core/modules/user/user.module index 5dacd11c307a686ee08c17485ef3af611701fe02..41dbae907c10cfd83913ef270370dd3e517930a5 100644 --- a/core/modules/user/user.module +++ b/core/modules/user/user.module @@ -537,7 +537,7 @@ function user_validate_current_pass(&$form, &$form_state) { // This validation only works for required textfields (like mail) or // form values like password_confirm that have their own validation // that prevent them from being empty if they are changed. - $current_value = $account->getPropertyDefinition($key) ? $account->get($key)->value : $account->$key; + $current_value = $account->hasField($key) ? $account->get($key)->value : $account->$key; if ((strlen(trim($form_state['values'][$key])) > 0) && ($form_state['values'][$key] != $current_value)) { $current_pass_failed = empty($form_state['values']['current_pass']) || !\Drupal::service('password')->check($form_state['values']['current_pass'], $account); if ($current_pass_failed) {