Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adding expandEntityBaseFields() method. #114

Merged
merged 3 commits into from
Mar 16, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
.idea
*.tgz
*.phar
composer.lock
Expand Down
9 changes: 6 additions & 3 deletions src/Drupal/Driver/Cores/AbstractCore.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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, array $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)
Expand Down
5 changes: 4 additions & 1 deletion src/Drupal/Driver/Cores/CoreInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -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, array $base_fields = array());

/**
* Creates a language.
Expand Down
9 changes: 8 additions & 1 deletion src/Drupal/Driver/Cores/Drupal6.php
Original file line number Diff line number Diff line change
Expand Up @@ -432,7 +432,14 @@ public function getExtensionPathList() {
/**
* {@inheritdoc}
*/
public function getEntityFieldTypes($entity_type) {
protected function expandEntityFields($entity_type, \stdClass $entity, array $base_fields = array()) {
return parent::expandEntityFields($entity_type, $entity);
}

/**
* {@inheritdoc}
*/
public function getEntityFieldTypes($entity_type, array $base_fields = array()) {
$taxonomy_fields = array('taxonomy' => 'taxonomy');
if (!module_exists('content')) {
return $taxonomy_fields;
Expand Down
2 changes: 1 addition & 1 deletion src/Drupal/Driver/Cores/Drupal7.php
Original file line number Diff line number Diff line change
Expand Up @@ -456,7 +456,7 @@ public function getExtensionPathList() {
/**
* {@inheritdoc}
*/
public function getEntityFieldTypes($entity_type) {
public function getEntityFieldTypes($entity_type, array $base_fields = array()) {
$return = array();
$fields = field_info_field_map();
foreach ($fields as $field_name => $field) {
Expand Down
28 changes: 26 additions & 2 deletions src/Drupal/Driver/Cores/Drupal8.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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, array $base_fields) {
$this->expandEntityFields($entity_type, $entity, $base_fields);
}

/**
* {@inheritdoc}
*/
public function getEntityFieldTypes($entity_type) {
public function getEntityFieldTypes($entity_type, array $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();
}
}
Expand All @@ -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}
*/
Expand Down
6 changes: 5 additions & 1 deletion src/Drupal/Driver/Fields/Drupal8/EntityReferenceHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -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');
Expand Down