Currently there's lots of code that checks whether a field exists by calling $entity->getPropertyDefinition(). Firstly, there's some terminology confusion around "property definition" vs. "field definition". #2047229: Make use of classes for entity field and data definitions might help resolve some of that. But secondly, module code shouldn't need to deal with the concept of a definition at all if all it wants to do is know whether the field exists.

So, this patch adds a hasField() method.

Files: 
CommentFileSizeAuthor
entity-has-field.patch10.15 KBeffulgentsia
PASSED: [[SimpleTest]]: [MySQL] 58,834 pass(es).
[ View ]

Comments

Yes, comment module could use this, as seen by the patch :)

Status:Needs review» Reviewed & tested by the community

Makes sense..

Status:Reviewed & tested by the community» Needs work

Awesome patch is green.

Before the patch

core/lib/Drupal/Core/Config/Schema/Mapping.php:118:      $list[$key] = $this->getPropertyDefinition($key);
core/lib/Drupal/Core/Entity/ContentEntityBase.php:389:      $definition = $this->getPropertyDefinition($property_name);
core/lib/Drupal/Core/Entity/ContentEntityBase.php:820:    elseif ($this->getPropertyDefinition($name)) {
core/lib/Drupal/Core/Entity/ContentEntityBase.php:839:    if ($this->getPropertyDefinition($name)) {
core/lib/Drupal/Core/Entity/ContentEntityBase.php:851:    if ($this->getPropertyDefinition($name)) {
core/lib/Drupal/Core/Entity/EntityRenderController.php:73:        if ($entity->getPropertyDefinition($name)) {
core/lib/Drupal/Core/Entity/Field/FieldItemList.php:152:    return $this->offsetGet(0)->getPropertyDefinition($name);
core/lib/Drupal/Core/TypedData/Plugin/DataType/Map.php:134:    if ($this->getPropertyDefinition($property_name)) {
core/lib/Drupal/Core/TypedData/TypedDataManager.php:232:        $definition = $object->getPropertyDefinition($property_name);
core/modules/comment/comment.module:481:    if (!$entity->getPropertyDefinition($field_name)) {
core/modules/comment/comment.module:1022:      if (!$entity->getPropertyDefinition($field_name)) {
core/modules/comment/comment.module:1105:      if (!$node->getPropertyDefinition($field_name)) {
core/modules/comment/comment.module:1142:    if (!$node->getPropertyDefinition($field_name)) {
core/modules/edit/edit.module:188:  $definition = $entity->getPropertyDefinition($element['#field_name']);
core/modules/hal/lib/Drupal/hal/Normalizer/FieldNormalizer.php:36:    $field_definition = $entity->getPropertyDefinition($field_name);
core/modules/user/user.module:540:    $current_value = $account->getPropertyDefinition($key) ? $account->get($key)->value : $account->$key;

After the patch

core/lib/Drupal/Core/Config/Schema/Mapping.php:118:      $list[$key] = $this->getPropertyDefinition($key);
core/lib/Drupal/Core/Entity/ContentEntityBase.php:370:    return (bool) $this->getPropertyDefinition($field_name);
core/lib/Drupal/Core/Entity/ContentEntityBase.php:396:      $definition = $this->getPropertyDefinition($property_name);
core/lib/Drupal/Core/Entity/Field/FieldItemList.php:152:    return $this->offsetGet(0)->getPropertyDefinition($name);
core/lib/Drupal/Core/TypedData/Plugin/DataType/Map.php:134:    if ($this->getPropertyDefinition($property_name)) {
core/lib/Drupal/Core/TypedData/TypedDataManager.php:232:        $definition = $object->getPropertyDefinition($property_name);
core/modules/edit/edit.module:188:  $definition = $entity->getPropertyDefinition($element['#field_name']);
core/modules/hal/lib/Drupal/hal/Normalizer/FieldNormalizer.php:36:    $field_definition = $entity->getPropertyDefinition($field_name);
core/lib/Drupal/Core/Entity/ContentEntityBase.php:549:      if ($this->getPropertyDefinition('langcode') && ($item = $this->get('langcode')) && isset($item->language)) {
core/modules/system/lib/Drupal/system/Tests/TypedData/TypedDataTest.php:425:    $definition = $typed_data->getPropertyDefinition('one');
core/modules/system/lib/Drupal/system/Tests/TypedData/TypedDataTest.php:427:    $this->assertFalse($typed_data->getPropertyDefinition('invalid'));
core/modules/system/lib/Drupal/system/Tests/TypedData/TypedDataTest.php:496:    $definition = $typed_data->getPropertyDefinition('zero');

core/lib/Drupal/Core/Entity/ContentEntityBase.php:370 is introduced by the patch I think we can fix the rest.

Status:Needs work» Needs review

The rest either deal with typed data objects that aren't entities, or else need the returned definition.

Status:Needs review» Reviewed & tested by the community

ok RTBC then.

That's a great addition and makes code better readable, +1!

Issue tags:+API addition

Nice! :)

Title:Add ContentEntityInterface::hasField()Change notice: Add ContentEntityInterface::hasField()
Status:Reviewed & tested by the community» Active
Issue tags:+Needs change record

This seems pretty harmless, and is a nice clean-up of code.

Committed and pushed to 8.x. Thanks!

Needs a change notice.

Title:Change notice: Add ContentEntityInterface::hasField()Add ContentEntityInterface::hasField()
Status:Active» Fixed
Issue tags:-Needs change record

Added a snippet to https://drupal.org/node/1806650.

Status:Fixed» Closed (fixed)

Automatically closed - issue fixed for 2 weeks with no activity.