From 53092f6123ef0770fda87088bb577f05c126a665 Mon Sep 17 00:00:00 2001 From: Matthew Grasmick Date: Mon, 18 Dec 2017 09:55:31 -0500 Subject: [PATCH 1/3] Resolving rebase conflicts. --- .gitignore | 1 + src/Drupal/Driver/Cores/AbstractCore.php | 9 ++++-- src/Drupal/Driver/Cores/CoreInterface.php | 5 +++- src/Drupal/Driver/Cores/Drupal6.php | 9 +++++- src/Drupal/Driver/Cores/Drupal7.php | 2 +- src/Drupal/Driver/Cores/Drupal8.php | 28 +++++++++++++++++-- .../Fields/Drupal8/EntityReferenceHandler.php | 6 +++- 7 files changed, 51 insertions(+), 9 deletions(-) diff --git a/.gitignore b/.gitignore index e97e1907..b9e60d31 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,4 @@ +.idea *.tgz *.phar composer.lock diff --git a/src/Drupal/Driver/Cores/AbstractCore.php b/src/Drupal/Driver/Cores/AbstractCore.php index 7de69196..6347ef88 100644 --- a/src/Drupal/Driver/Cores/AbstractCore.php +++ b/src/Drupal/Driver/Cores/AbstractCore.php @@ -56,7 +56,7 @@ public function getRandom() { public function getFieldHandler($entity, $entity_type, $field_name) { $reflection = new \ReflectionClass($this); $core_namespace = $reflection->getShortName(); - $field_types = $this->getEntityFieldTypes($entity_type); + $field_types = $this->getEntityFieldTypes($entity_type, array($field_name)); $camelized_type = Container::camelize($field_types[$field_name]); $default_class = sprintf('\Drupal\Driver\Fields\%s\DefaultHandler', $core_namespace); $class_name = sprintf('\Drupal\Driver\Fields\%s\%sHandler', $core_namespace, $camelized_type); @@ -73,9 +73,12 @@ public function getFieldHandler($entity, $entity_type, $field_name) { * The entity type ID. * @param \stdClass $entity * Entity object. + * @param array $base_fields + * Optional. Define base fields that will be expanded in addition to user + * defined fields. */ - protected function expandEntityFields($entity_type, \stdClass $entity) { - $field_types = $this->getEntityFieldTypes($entity_type); + protected function expandEntityFields($entity_type, \stdClass $entity, $base_fields = array()) { + $field_types = $this->getEntityFieldTypes($entity_type, $base_fields); foreach ($field_types as $field_name => $type) { if (isset($entity->$field_name)) { $entity->$field_name = $this->getFieldHandler($entity, $entity_type, $field_name) diff --git a/src/Drupal/Driver/Cores/CoreInterface.php b/src/Drupal/Driver/Cores/CoreInterface.php index 303c5f90..b3ad67d7 100644 --- a/src/Drupal/Driver/Cores/CoreInterface.php +++ b/src/Drupal/Driver/Cores/CoreInterface.php @@ -164,11 +164,14 @@ public function isField($entity_type, $field_name); * * @param string $entity_type * The entity type for which to return the field types. + * @param array $base_fields + * Optional. Define base fields that will be returned in addition to user- + * defined fields. * * @return array * An associative array of field types, keyed by field name. */ - public function getEntityFieldTypes($entity_type); + public function getEntityFieldTypes($entity_type, $base_fields = array()); /** * Creates a language. diff --git a/src/Drupal/Driver/Cores/Drupal6.php b/src/Drupal/Driver/Cores/Drupal6.php index 97026ea8..60ca0b2b 100644 --- a/src/Drupal/Driver/Cores/Drupal6.php +++ b/src/Drupal/Driver/Cores/Drupal6.php @@ -432,7 +432,14 @@ public function getExtensionPathList() { /** * {@inheritdoc} */ - public function getEntityFieldTypes($entity_type) { + protected function expandEntityFields($entity_type, \stdClass $entity, $base_fields = array()) { + return parent::expandEntityFields($entity_type, $entity); + } + + /** + * {@inheritdoc} + */ + public function getEntityFieldTypes($entity_type, $base_fields = array()) { $taxonomy_fields = array('taxonomy' => 'taxonomy'); if (!module_exists('content')) { return $taxonomy_fields; diff --git a/src/Drupal/Driver/Cores/Drupal7.php b/src/Drupal/Driver/Cores/Drupal7.php index bdbe1df4..560d553a 100644 --- a/src/Drupal/Driver/Cores/Drupal7.php +++ b/src/Drupal/Driver/Cores/Drupal7.php @@ -456,7 +456,7 @@ public function getExtensionPathList() { /** * {@inheritdoc} */ - public function getEntityFieldTypes($entity_type) { + public function getEntityFieldTypes($entity_type, $base_fields = array()) { $return = array(); $fields = field_info_field_map(); foreach ($fields as $field_name => $field) { diff --git a/src/Drupal/Driver/Cores/Drupal8.php b/src/Drupal/Driver/Cores/Drupal8.php index 108a44e4..bd5c062b 100644 --- a/src/Drupal/Driver/Cores/Drupal8.php +++ b/src/Drupal/Driver/Cores/Drupal8.php @@ -3,6 +3,7 @@ namespace Drupal\Driver\Cores; use Drupal\Core\DrupalKernel; +use Drupal\Core\Field\BaseFieldDefinition; use Drupal\Driver\Exception\BootstrapException; use Drupal\field\Entity\FieldStorageConfig; use Drupal\language\Entity\ConfigurableLanguage; @@ -357,14 +358,29 @@ public function getExtensionPathList() { return $paths; } + /** + * Expands specified base fields on the entity object. + * + * @param string $entity_type + * The entity type for which to return the field types. + * @param \stdClass $entity + * Entity object. + * @param array $base_fields + * Base fields to be expanded in addition to user defined fields. + */ + public function expandEntityBaseFields($entity_type, \stdClass $entity, $base_fields) { + $this->expandEntityFields($entity_type, $entity, $base_fields); + } + /** * {@inheritdoc} */ - public function getEntityFieldTypes($entity_type) { + public function getEntityFieldTypes($entity_type, $base_fields = array()) { $return = array(); $fields = \Drupal::entityManager()->getFieldStorageDefinitions($entity_type); foreach ($fields as $field_name => $field) { - if ($this->isField($entity_type, $field_name)) { + if ($this->isField($entity_type, $field_name) + || (in_array($field_name, $base_fields) && $this->isBaseField($entity_type, $field_name))) { $return[$field_name] = $field->getType(); } } @@ -379,6 +395,14 @@ public function isField($entity_type, $field_name) { return (isset($fields[$field_name]) && $fields[$field_name] instanceof FieldStorageConfig); } + /** + * {@inheritdoc} + */ + public function isBaseField($entity_type, $field_name) { + $fields = \Drupal::entityManager()->getFieldStorageDefinitions($entity_type); + return (isset($fields[$field_name]) && $fields[$field_name] instanceof BaseFieldDefinition); + } + /** * {@inheritdoc} */ diff --git a/src/Drupal/Driver/Fields/Drupal8/EntityReferenceHandler.php b/src/Drupal/Driver/Fields/Drupal8/EntityReferenceHandler.php index 388e3c54..84164344 100644 --- a/src/Drupal/Driver/Fields/Drupal8/EntityReferenceHandler.php +++ b/src/Drupal/Driver/Fields/Drupal8/EntityReferenceHandler.php @@ -24,13 +24,17 @@ public function expand($values) { $label_key = 'name'; } + if (!$label_key && $entity_type_id == 'user') { + $label_key = 'name'; + } + // Determine target bundle restrictions. $target_bundle_key = NULL; if ($target_bundles = $this->getTargetBundles()) { $target_bundle_key = $entity_definition->getKey('bundle'); } - foreach ($values as $value) { + foreach ((array) $values as $value) { $query = \Drupal::entityQuery($entity_type_id)->condition($label_key, $value); if ($target_bundles && $target_bundle_key) { $query->condition($target_bundle_key, $target_bundles, 'IN'); From 33327eb27ef74afc0f5ff9525dfc6b551375381c Mon Sep 17 00:00:00 2001 From: Matthew Grasmick Date: Mon, 18 Dec 2017 09:58:34 -0500 Subject: [PATCH 2/3] PHPCS fixes. --- src/Drupal/Driver/Cores/AbstractCore.php | 2 +- src/Drupal/Driver/Cores/CoreInterface.php | 2 +- src/Drupal/Driver/Cores/Drupal8.php | 2 +- 3 files changed, 3 insertions(+), 3 deletions(-) diff --git a/src/Drupal/Driver/Cores/AbstractCore.php b/src/Drupal/Driver/Cores/AbstractCore.php index 6347ef88..e77cfbe8 100644 --- a/src/Drupal/Driver/Cores/AbstractCore.php +++ b/src/Drupal/Driver/Cores/AbstractCore.php @@ -77,7 +77,7 @@ public function getFieldHandler($entity, $entity_type, $field_name) { * Optional. Define base fields that will be expanded in addition to user * defined fields. */ - protected function expandEntityFields($entity_type, \stdClass $entity, $base_fields = array()) { + protected function expandEntityFields($entity_type, \stdClass $entity, array $base_fields = array()) { $field_types = $this->getEntityFieldTypes($entity_type, $base_fields); foreach ($field_types as $field_name => $type) { if (isset($entity->$field_name)) { diff --git a/src/Drupal/Driver/Cores/CoreInterface.php b/src/Drupal/Driver/Cores/CoreInterface.php index b3ad67d7..6adf2be9 100644 --- a/src/Drupal/Driver/Cores/CoreInterface.php +++ b/src/Drupal/Driver/Cores/CoreInterface.php @@ -171,7 +171,7 @@ public function isField($entity_type, $field_name); * @return array * An associative array of field types, keyed by field name. */ - public function getEntityFieldTypes($entity_type, $base_fields = array()); + public function getEntityFieldTypes($entity_type, array $base_fields = array()); /** * Creates a language. diff --git a/src/Drupal/Driver/Cores/Drupal8.php b/src/Drupal/Driver/Cores/Drupal8.php index bd5c062b..e524616f 100644 --- a/src/Drupal/Driver/Cores/Drupal8.php +++ b/src/Drupal/Driver/Cores/Drupal8.php @@ -368,7 +368,7 @@ public function getExtensionPathList() { * @param array $base_fields * Base fields to be expanded in addition to user defined fields. */ - public function expandEntityBaseFields($entity_type, \stdClass $entity, $base_fields) { + public function expandEntityBaseFields($entity_type, \stdClass $entity, array $base_fields) { $this->expandEntityFields($entity_type, $entity, $base_fields); } From fae35a45f4966b43c00d71f746586937cf0d5092 Mon Sep 17 00:00:00 2001 From: Matthew Grasmick Date: Mon, 18 Dec 2017 10:03:43 -0500 Subject: [PATCH 3/3] Fixing core function signatures. --- src/Drupal/Driver/Cores/Drupal6.php | 4 ++-- src/Drupal/Driver/Cores/Drupal7.php | 2 +- src/Drupal/Driver/Cores/Drupal8.php | 2 +- 3 files changed, 4 insertions(+), 4 deletions(-) diff --git a/src/Drupal/Driver/Cores/Drupal6.php b/src/Drupal/Driver/Cores/Drupal6.php index 60ca0b2b..3967e841 100644 --- a/src/Drupal/Driver/Cores/Drupal6.php +++ b/src/Drupal/Driver/Cores/Drupal6.php @@ -432,14 +432,14 @@ public function getExtensionPathList() { /** * {@inheritdoc} */ - protected function expandEntityFields($entity_type, \stdClass $entity, $base_fields = array()) { + protected function expandEntityFields($entity_type, \stdClass $entity, array $base_fields = array()) { return parent::expandEntityFields($entity_type, $entity); } /** * {@inheritdoc} */ - public function getEntityFieldTypes($entity_type, $base_fields = array()) { + public function getEntityFieldTypes($entity_type, array $base_fields = array()) { $taxonomy_fields = array('taxonomy' => 'taxonomy'); if (!module_exists('content')) { return $taxonomy_fields; diff --git a/src/Drupal/Driver/Cores/Drupal7.php b/src/Drupal/Driver/Cores/Drupal7.php index 560d553a..eb46b873 100644 --- a/src/Drupal/Driver/Cores/Drupal7.php +++ b/src/Drupal/Driver/Cores/Drupal7.php @@ -456,7 +456,7 @@ public function getExtensionPathList() { /** * {@inheritdoc} */ - public function getEntityFieldTypes($entity_type, $base_fields = array()) { + public function getEntityFieldTypes($entity_type, array $base_fields = array()) { $return = array(); $fields = field_info_field_map(); foreach ($fields as $field_name => $field) { diff --git a/src/Drupal/Driver/Cores/Drupal8.php b/src/Drupal/Driver/Cores/Drupal8.php index e524616f..9fa8a1b5 100644 --- a/src/Drupal/Driver/Cores/Drupal8.php +++ b/src/Drupal/Driver/Cores/Drupal8.php @@ -375,7 +375,7 @@ public function expandEntityBaseFields($entity_type, \stdClass $entity, array $b /** * {@inheritdoc} */ - public function getEntityFieldTypes($entity_type, $base_fields = array()) { + public function getEntityFieldTypes($entity_type, array $base_fields = array()) { $return = array(); $fields = \Drupal::entityManager()->getFieldStorageDefinitions($entity_type); foreach ($fields as $field_name => $field) {