Issue #2112759 by effulgentsia: Add ContentEntityInterface::hasField().

8.0.x
webchick 2013-10-17 23:16:26 -07:00
parent 59b3495a49
commit d0d5f6be9b
8 changed files with 34 additions and 16 deletions

View File

@ -363,6 +363,13 @@ abstract class ContentEntityBase extends Entity implements \IteratorAggregate, C
return $this->get('uuid')->value;
}
/**
* {@inheritdoc}
*/
public function hasField($field_name) {
return (bool) $this->getPropertyDefinition($field_name);
}
/**
* {@inheritdoc}
*/
@ -817,7 +824,7 @@ abstract class ContentEntityBase extends Entity implements \IteratorAggregate, C
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 @@ abstract class ContentEntityBase extends Entity implements \IteratorAggregate, C
* 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 @@ abstract class ContentEntityBase extends Entity implements \IteratorAggregate, C
* Implements the magic method for unset.
*/
public function __unset($name) {
if ($this->getPropertyDefinition($name)) {
if ($this->hasField($name)) {
$this->get($name)->setValue(NULL);
}
else {

View File

@ -55,4 +55,15 @@ interface ContentEntityInterface extends EntityInterface, RevisionableInterface,
*/
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);
}

View File

@ -70,7 +70,7 @@ class EntityRenderController implements EntityRenderControllerInterface {
// 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();
}

View File

@ -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';
}
}

View File

@ -108,7 +108,7 @@ class CommentStorageController extends FieldableDatabaseStorageController implem
// @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)

View File

@ -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']));

View File

@ -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');
}

View File

@ -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) {