security updates

have to check views and entityreference for custom patches
This commit is contained in:
Bachir Soussi Chiadmi
2015-04-19 20:45:16 +02:00
parent 802ec0c6f3
commit b3221c71e2
516 changed files with 14267 additions and 7349 deletions

View File

@@ -1,93 +0,0 @@
diff --git a/plugins/selection/EntityReference_SelectionHandler_Generic.class.php b/plugins/selection/EntityReference_SelectionHandler_Generic.class.php
index 7db4045..f3ff067 100644
--- a/plugins/selection/EntityReference_SelectionHandler_Generic.class.php
+++ b/plugins/selection/EntityReference_SelectionHandler_Generic.class.php
@@ -307,6 +307,27 @@ class EntityReference_SelectionHandler_Generic_node extends EntityReference_Sele
* This only exists to workaround core bugs.
*/
class EntityReference_SelectionHandler_Generic_user extends EntityReference_SelectionHandler_Generic {
+ /**
+ * Implements EntityReferenceHandler::settingsForm().
+ */
+ public static function settingsForm($field, $instance) {
+ $settings = $field['settings']['handler_settings'];
+ $form = parent::settingsForm($field, $instance);
+ $form['referenceable_roles'] = array(
+ '#type' => 'checkboxes',
+ '#title' => t('User roles that can be referenced'),
+ '#default_value' => isset($settings['referenceable_roles']) ? array_filter($settings['referenceable_roles']) : array(),
+ '#options' => user_roles(TRUE),
+ );
+ $form['referenceable_status'] = array(
+ '#type' => 'checkboxes',
+ '#title' => t('User status that can be referenced'),
+ '#default_value' => isset($settings['referenceable_status']) ? array_filter($settings['referenceable_status']) : array('active' => 'active'),
+ '#options' => array('active' => t('Active'), 'blocked' => t('Blocked')),
+ );
+ return $form;
+ }
+
public function buildEntityFieldQuery($match = NULL, $match_operator = 'CONTAINS') {
$query = parent::buildEntityFieldQuery($match, $match_operator);
@@ -315,21 +336,33 @@ class EntityReference_SelectionHandler_Generic_user extends EntityReference_Sele
$query->propertyCondition('name', $match, $match_operator);
}
- // Adding the 'user_access' tag is sadly insufficient for users: core
- // requires us to also know about the concept of 'blocked' and
- // 'active'.
- if (!user_access('administer users')) {
- $query->propertyCondition('status', 1);
+ $field = $this->field;
+ $settings = $field['settings']['handler_settings'];
+ $referenceable_roles = isset($settings['referenceable_roles']) ? array_filter($settings['referenceable_roles']) : array();
+ $referenceable_status = isset($settings['referenceable_status']) ? array_filter($settings['referenceable_status']) : array('active' => 'active');
+
+ // If this filter is not filled, use the users access permissions.
+ if (empty($referenceable_status)) {
+ // Adding the 'user_access' tag is sadly insufficient for users: core
+ // requires us to also know about the concept of 'blocked' and 'active'.
+ if (!user_access('administer users')) {
+ $query->propertyCondition('status', 1);
+ }
+ }
+ elseif (count($referenceable_status) == 1) {
+ $values = array('active' => 1, 'blocked' => 0);
+ $query->propertyCondition('status', $values[key($referenceable_status)]);
}
+
return $query;
}
public function entityFieldQueryAlter(SelectQueryInterface $query) {
+ $conditions = &$query->conditions();
if (user_access('administer users')) {
- // In addition, if the user is administrator, we need to make sure to
+ // If the user is administrator, we need to make sure to
// match the anonymous user, that doesn't actually have a name in the
// database.
- $conditions = &$query->conditions();
foreach ($conditions as $key => $condition) {
if ($condition['field'] == 'users.name') {
// Remove the condition.
@@ -356,6 +389,19 @@ class EntityReference_SelectionHandler_Generic_user extends EntityReference_Sele
}
}
}
+
+ $field = $this->field;
+ $settings = $field['settings']['handler_settings'];
+ $referenceable_roles = isset($settings['referenceable_roles']) ? array_filter($settings['referenceable_roles']) : array();
+ if (!$referenceable_roles || !empty($referenceable_roles[DRUPAL_AUTHENTICATED_RID])) {
+ // Return early if "authenticated user" choosen.
+ return;
+ }
+
+ if (!isset($referenceable_roles[DRUPAL_AUTHENTICATED_RID])) {
+ $query->join('users_roles', 'users_roles', 'users.uid = users_roles.uid');
+ $query->condition('users_roles.rid', array_keys($referenceable_roles), 'IN');
+ }
}
}

View File

@@ -20,7 +20,9 @@ function _entityreference_devel_generate($object, $field, $instance, $bundle) {
$referencable_entity = entityreference_get_selection_handler($field, $instance)->getReferencableEntities();
if (is_array($referencable_entity) && !empty($referencable_entity)) {
// Get a random key.
$object_field['target_id'] = array_rand($referencable_entity);
foreach ($referencable_entity as $type => $eids) {
$object_field['target_id'] = array_rand($eids);
}
}
return $object_field;
}

View File

@@ -15,14 +15,42 @@ function entityreference_feeds_processor_targets_alter(&$targets, $entity_type,
foreach (field_info_instances($entity_type, $bundle_name) as $name => $instance) {
$info = field_info_field($name);
if ($info['type'] == 'entityreference') {
// We don't use ":guid" in key, not to break existing configurations.
$targets[$name] = array(
'name' => check_plain($instance['label']),
'name' => check_plain($instance['label'] . t(' (Entity reference by Feeds GUID)')),
'callback' => 'entityreference_feeds_set_target',
'description' => t('The field instance @label of @id', array(
'description' => t('The field instance @label of @id matched by Feeds GUID.', array(
'@label' => $instance['label'],
'@id' => $name,
)),
);
$targets[$name . ':url'] = array(
'name' => check_plain($instance['label'] . t(' (Entity reference by Feeds URL)')),
'callback' => 'entityreference_feeds_set_target',
'description' => t('The field instance @label of @id matched by Feeds URL.', array(
'@label' => $instance['label'],
'@id' => $name,
)),
'real_target' => $name,
);
$targets[$name . ':etid'] = array(
'name' => check_plain($instance['label'] . t(' (Entity reference by Entity ID)')),
'callback' => 'entityreference_feeds_set_target',
'description' => t('The field instance @label of @id matched by Entity ID.', array(
'@label' => $instance['label'],
'@id' => $name,
)),
'real_target' => $name,
);
$targets[$name . ':label'] = array(
'name' => check_plain($instance['label'] . t(' (Entity reference by Entity label)')),
'callback' => 'entityreference_feeds_set_target',
'description' => t('The field instance @label of @id matched by Entity label.', array(
'@label' => $instance['label'],
'@id' => $name,
)),
'real_target' => $name,
);
}
}
}
@@ -42,12 +70,8 @@ function entityreference_feeds_processor_targets_alter(&$targets, $entity_type,
* The target key on $entity to map to.
* @param $value
* The value to map. MUST be an array.
* @param $mapping
* Array of mapping settings for current value.
* @param $input_format
* TRUE if an input format should be applied.
*/
function entityreference_feeds_set_target($source, $entity, $target, $value, $mapping, $input_format = FALSE) {
function entityreference_feeds_set_target($source, $entity, $target, $value) {
// Don't do anything if we weren't given any data.
if (empty($value)) {
@@ -62,8 +86,19 @@ function entityreference_feeds_set_target($source, $entity, $target, $value, $ma
$values = array($value);
}
// Determine the field we are matching against.
if (strpos($target, ':') === FALSE) {
$match_key = 'guid';
}
else {
list($target, $match_key) = explode(':', $target, 2);
}
// Get some useful field information.
$info = field_info_field($target);
if ($match_key == 'label') {
$handler = entityreference_get_selection_handler($info);
}
// Set the language of the field depending on the mapping.
$language = isset($mapping['language']) ? $mapping['language'] : LANGUAGE_NONE;
@@ -75,13 +110,27 @@ function entityreference_feeds_set_target($source, $entity, $target, $value, $ma
// Only process if this value was set for this instance.
if ($value) {
// Fetch the entity ID resulting from the mapping table look-up.
$entity_id = db_query(
'SELECT entity_id FROM {feeds_item} WHERE guid = :guid',
array(':guid' => $value)
)->fetchField();
switch ($match_key) {
case 'guid':
case 'url':
// Fetch the entity ID resulting from the mapping table look-up.
$entity_id = db_select('feeds_item', 'fi')
->fields('fi', array('entity_id'))
->condition($match_key, $value,'=')
->execute()
->fetchField();
break;
case 'etid':
$entity_id = $value;
break;
case 'label':
$options = $handler->getReferencableEntities($value, '=');
$options = reset($options);
$etids = array_keys($options);
// Use the first matching entity.
$entity_id = reset($etids);
break;
}
/*
* Only add a reference to an existing entity ID if there exists a
* mapping between it and the provided GUID. In cases where no such
@@ -106,6 +155,7 @@ function entityreference_feeds_set_target($source, $entity, $target, $value, $ma
* this opportunity later, we need to destroy the hash.
*/
unset($entity->feeds_item->hash);
$source->log('entityreference', t('No existing entity found for entity @source_id entityreference to source entity @value', array('@source_id' => $entity->feeds_item->entity_id, '@value' => $value)));
}
}

View File

@@ -21,10 +21,11 @@ files[] = views/entityreference_plugin_row_fields.inc
files[] = tests/entityreference.handlers.test
files[] = tests/entityreference.taxonomy.test
files[] = tests/entityreference.admin.test
files[] = tests/entityreference.feeds.test
; Information added by drupal.org packaging script on 2012-11-18
version = "7.x-1.0"
; Information added by packaging script on 2013-11-20
version = "7.x-1.1"
core = "7.x"
project = "entityreference"
datestamp = "1353230808"
datestamp = "1384973110"

View File

@@ -11,7 +11,7 @@
function entityreference_migrate_api() {
return array(
'api' => 2,
'field_handlers' => array('MigrateEntityReferenceFieldHandler'),
'field handlers' => array('MigrateEntityReferenceFieldHandler'),
);
}

View File

@@ -791,6 +791,11 @@ function entityreference_query_entityreference_alter(QueryAlterableInterface $qu
* Implements hook_field_widget_form().
*/
function entityreference_field_widget_form(&$form, &$form_state, $field, $instance, $langcode, $items, $delta, $element) {
// Ensure that the entity target type exists before displaying the widget.
$entity_info = entity_get_info($field['settings']['target_type']);
if (empty($entity_info)){
return;
}
$entity_type = $instance['entity_type'];
$entity = isset($element['#entity']) ? $element['#entity'] : NULL;
$handler = entityreference_get_selection_handler($field, $instance, $entity_type, $entity);
@@ -975,6 +980,18 @@ function entityreference_autocomplete_access_callback($type, $field_name, $entit
* The label of the entity to query by.
*/
function entityreference_autocomplete_callback($type, $field_name, $entity_type, $bundle_name, $entity_id = '', $string = '') {
// If the request has a '/' in the search text, then the menu system will have
// split it into multiple arguments and $string will only be a partial. We want
// to make sure we recover the intended $string.
$args = func_get_args();
// Shift off the $type, $field_name, $entity_type, $bundle_name, and $entity_id args.
array_shift($args);
array_shift($args);
array_shift($args);
array_shift($args);
array_shift($args);
$string = implode('/', $args);
$field = field_info_field($field_name);
$instance = field_info_instance($entity_type, $field_name, $bundle_name);
@@ -1007,7 +1024,9 @@ function entityreference_autocomplete_callback_get_matches($type, $field, $insta
$entity = NULL;
if ($entity_id !== 'NULL') {
$entity = entity_load_single($entity_type, $entity_id);
if (!$entity || !entity_access('view', $entity_type, $entity)) {
$has_view_access = (entity_access('view', $entity_type, $entity) !== FALSE);
$has_update_access = (entity_access('update', $entity_type, $entity) !== FALSE);
if (!$entity || !($has_view_access || $has_update_access)) {
return MENU_ACCESS_DENIED;
}
}
@@ -1073,7 +1092,7 @@ function entityreference_field_formatter_info() {
'description' => t('Display the referenced entities rendered by entity_view().'),
'field types' => array('entityreference'),
'settings' => array(
'view_mode' => '',
'view_mode' => 'default',
'links' => TRUE,
),
),
@@ -1097,21 +1116,20 @@ function entityreference_field_formatter_settings_form($field, $instance, $view_
if ($display['type'] == 'entityreference_entity_view') {
$entity_info = entity_get_info($field['settings']['target_type']);
$options = array();
$options = array('default' => t('Default'));
if (!empty($entity_info['view modes'])) {
foreach ($entity_info['view modes'] as $view_mode => $view_mode_settings) {
$options[$view_mode] = $view_mode_settings['label'];
}
}
if (count($options) > 1) {
$element['view_mode'] = array(
'#type' => 'select',
'#options' => $options,
'#title' => t('View mode'),
'#default_value' => $settings['view_mode'],
);
}
$element['view_mode'] = array(
'#type' => 'select',
'#options' => $options,
'#title' => t('View mode'),
'#default_value' => $settings['view_mode'],
'#access' => count($options) > 1,
);
$element['links'] = array(
'#type' => 'checkbox',
@@ -1138,7 +1156,11 @@ function entityreference_field_formatter_settings_summary($field, $instance, $vi
if ($display['type'] == 'entityreference_entity_view') {
$entity_info = entity_get_info($field['settings']['target_type']);
$summary[] = t('Rendered as @mode', array('@mode' => isset($entity_info['view modes'][$settings['view_mode']]['label']) ? $entity_info['view modes'][$settings['view_mode']]['label'] : $settings['view_mode']));
$view_mode_label = $settings['view_mode'] == 'default' ? t('Default') : $settings['view_mode'];
if (isset($entity_info['view modes'][$settings['view_mode']]['label'])) {
$view_mode_label = $entity_info['view modes'][$settings['view_mode']]['label'];
}
$summary[] = t('Rendered as @mode', array('@mode' => $view_mode_label));
$summary[] = !empty($settings['links']) ? t('Display links') : t('Do not display links');
}
@@ -1177,7 +1199,9 @@ function entityreference_field_formatter_prepare_view($entity_type, $entities, $
// Replace the instance value with the term data.
$items[$id][$delta]['entity'] = $target_entities[$item['target_id']];
// Check whether the user has access to the referenced entity.
$items[$id][$delta]['access'] = entity_access('view', $field['settings']['target_type'], $target_entities[$item['target_id']]);
$has_view_access = (entity_access('view', $field['settings']['target_type'], $target_entities[$item['target_id']]) !== FALSE);
$has_update_access = (entity_access('update', $field['settings']['target_type'], $target_entities[$item['target_id']]) !== FALSE);
$items[$id][$delta]['access'] = ($has_view_access || $has_update_access);
}
// Otherwise, unset the instance value, since the entity does not exist.
else {

View File

@@ -4,9 +4,9 @@ core = 7.x
package = Fields
dependencies[] = entityreference
; Information added by drupal.org packaging script on 2012-11-18
version = "7.x-1.0"
; Information added by packaging script on 2013-11-20
version = "7.x-1.1"
core = "7.x"
project = "entityreference"
datestamp = "1353230808"
datestamp = "1384973110"

View File

@@ -1,5 +0,0 @@
1354482-er-user-roles-19.patch
http://drupal.org/node/1354482
patch-entityreference-7.x.patch
http://drupal.org/node/1691612

View File

@@ -1,18 +0,0 @@
diff --git a/views/entityreference_plugin_display.inc b/views/entityreference_plugin_display.inc
index 1fc6450..b9a956d 100644
--- a/views/entityreference_plugin_display.inc
+++ b/views/entityreference_plugin_display.inc
@@ -76,7 +76,12 @@
foreach ($style_options['search_fields'] as $field_alias) {
if (!empty($field_alias)) {
// Get the table and field names for the checked field
- $field = $this->view->query->fields[$this->view->field[$field_alias]->field_alias];
+ if (empty($this->view->field[$field_alias]->field_info))
+ $field = $this->view->query->fields[$this->view->field[$field_alias]->field_alias];
+ else {
+ $this->view->query->add_field($this->view->field[$field_alias]->options['table'], $this->view->field[$field_alias]->real_field, $this->view->field[$field_alias]->options['field'], array());
+ $field = $this->view->query->fields[$this->view->field[$field_alias]->options['field']];
+ }
// Add an OR condition for the field
$conditions->condition($field['table'] . '.' . $field['field'], $value, 'LIKE');
}

View File

@@ -304,7 +304,8 @@ class EntityReference_SelectionHandler_Generic implements EntityReference_Select
* Implements EntityReferenceHandler::getLabel().
*/
public function getLabel($entity) {
return entity_label($this->field['settings']['target_type'], $entity);
$target_type = $this->field['settings']['target_type'];
return entity_access('view', $target_type, $entity) ? entity_label($target_type, $entity) : t('- Restricted access -');
}
/**
@@ -340,7 +341,7 @@ class EntityReference_SelectionHandler_Generic implements EntityReference_Select
$entity_info = entity_get_info($target_type);
$id = $entity_info['entity keys']['id'];
// Return the alias of the table.
return $query->innerJoin($target_type, NULL, "$target_type.$id = $alias.entity_id");
return $query->innerJoin($target_type, NULL, "%alias.$id = $alias.entity_id");
}
}
@@ -369,27 +370,6 @@ class EntityReference_SelectionHandler_Generic_node extends EntityReference_Sele
* This only exists to workaround core bugs.
*/
class EntityReference_SelectionHandler_Generic_user extends EntityReference_SelectionHandler_Generic {
/**
* Implements EntityReferenceHandler::settingsForm().
*/
public static function settingsForm($field, $instance) {
$settings = $field['settings']['handler_settings'];
$form = parent::settingsForm($field, $instance);
$form['referenceable_roles'] = array(
'#type' => 'checkboxes',
'#title' => t('User roles that can be referenced'),
'#default_value' => isset($settings['referenceable_roles']) ? array_filter($settings['referenceable_roles']) : array(),
'#options' => user_roles(TRUE),
);
$form['referenceable_status'] = array(
'#type' => 'checkboxes',
'#title' => t('User status that can be referenced'),
'#default_value' => isset($settings['referenceable_status']) ? array_filter($settings['referenceable_status']) : array('active' => 'active'),
'#options' => array('active' => t('Active'), 'blocked' => t('Blocked')),
);
return $form;
}
public function buildEntityFieldQuery($match = NULL, $match_operator = 'CONTAINS') {
$query = parent::buildEntityFieldQuery($match, $match_operator);
@@ -398,33 +378,21 @@ class EntityReference_SelectionHandler_Generic_user extends EntityReference_Sele
$query->propertyCondition('name', $match, $match_operator);
}
$field = $this->field;
$settings = $field['settings']['handler_settings'];
$referenceable_roles = isset($settings['referenceable_roles']) ? array_filter($settings['referenceable_roles']) : array();
$referenceable_status = isset($settings['referenceable_status']) ? array_filter($settings['referenceable_status']) : array('active' => 'active');
// If this filter is not filled, use the users access permissions.
if (empty($referenceable_status)) {
// Adding the 'user_access' tag is sadly insufficient for users: core
// requires us to also know about the concept of 'blocked' and 'active'.
if (!user_access('administer users')) {
$query->propertyCondition('status', 1);
}
// Adding the 'user_access' tag is sadly insufficient for users: core
// requires us to also know about the concept of 'blocked' and
// 'active'.
if (!user_access('administer users')) {
$query->propertyCondition('status', 1);
}
elseif (count($referenceable_status) == 1) {
$values = array('active' => 1, 'blocked' => 0);
$query->propertyCondition('status', $values[key($referenceable_status)]);
}
return $query;
}
public function entityFieldQueryAlter(SelectQueryInterface $query) {
$conditions = &$query->conditions();
if (user_access('administer users')) {
// If the user is administrator, we need to make sure to
// In addition, if the user is administrator, we need to make sure to
// match the anonymous user, that doesn't actually have a name in the
// database.
$conditions = &$query->conditions();
foreach ($conditions as $key => $condition) {
if ($key !== '#conjunction' && is_string($condition['field']) && $condition['field'] === 'users.name') {
// Remove the condition.
@@ -451,19 +419,6 @@ class EntityReference_SelectionHandler_Generic_user extends EntityReference_Sele
}
}
}
$field = $this->field;
$settings = $field['settings']['handler_settings'];
$referenceable_roles = isset($settings['referenceable_roles']) ? array_filter($settings['referenceable_roles']) : array();
if (!$referenceable_roles || !empty($referenceable_roles[DRUPAL_AUTHENTICATED_RID])) {
// Return early if "authenticated user" choosen.
return;
}
if (!isset($referenceable_roles[DRUPAL_AUTHENTICATED_RID])) {
$query->join('users_roles', 'users_roles', 'users.uid = users_roles.uid');
$query->condition('users_roles.rid', array_keys($referenceable_roles), 'IN');
}
}
}
@@ -586,7 +541,7 @@ class EntityReference_SelectionHandler_Generic_taxonomy_term extends EntityRefer
foreach ($bundles as $bundle) {
if ($vocabulary = taxonomy_vocabulary_machine_name_load($bundle)) {
if ($terms = taxonomy_get_tree($vocabulary->vid, 0)) {
if ($terms = taxonomy_get_tree($vocabulary->vid, 0, NULL, TRUE)) {
foreach ($terms as $term) {
$options[$vocabulary->machine_name][$term->tid] = str_repeat('-', $term->depth) . check_plain($term->name);
}

View File

@@ -1,553 +0,0 @@
<?php
/**
* A generic Entity handler.
*
* The generic base implementation has a variety of overrides to workaround
* core's largely deficient entity handling.
*/
class EntityReference_SelectionHandler_Generic implements EntityReference_SelectionHandler {
/**
* Implements EntityReferenceHandler::getInstance().
*/
public static function getInstance($field, $instance = NULL, $entity_type = NULL, $entity = NULL) {
$target_entity_type = $field['settings']['target_type'];
// Check if the entity type does exist and has a base table.
$entity_info = entity_get_info($target_entity_type);
if (empty($entity_info['base table'])) {
return EntityReference_SelectionHandler_Broken::getInstance($field, $instance);
}
if (class_exists($class_name = 'EntityReference_SelectionHandler_Generic_' . $target_entity_type)) {
return new $class_name($field, $instance, $entity_type, $entity);
}
else {
return new EntityReference_SelectionHandler_Generic($field, $instance, $entity_type, $entity);
}
}
protected function __construct($field, $instance = NULL, $entity_type = NULL, $entity = NULL) {
$this->field = $field;
$this->instance = $instance;
$this->entity_type = $entity_type;
$this->entity = $entity;
}
/**
* Implements EntityReferenceHandler::settingsForm().
*/
public static function settingsForm($field, $instance) {
$entity_info = entity_get_info($field['settings']['target_type']);
// Merge-in default values.
$field['settings']['handler_settings'] += array(
'target_bundles' => array(),
'sort' => array(
'type' => 'none',
)
);
if (!empty($entity_info['entity keys']['bundle'])) {
$bundles = array();
foreach ($entity_info['bundles'] as $bundle_name => $bundle_info) {
$bundles[$bundle_name] = $bundle_info['label'];
}
$form['target_bundles'] = array(
'#type' => 'checkboxes',
'#title' => t('Target bundles'),
'#options' => $bundles,
'#default_value' => $field['settings']['handler_settings']['target_bundles'],
'#size' => 6,
'#multiple' => TRUE,
'#description' => t('The bundles of the entity type that can be referenced. Optional, leave empty for all bundles.'),
'#element_validate' => array('_entityreference_element_validate_filter'),
);
}
else {
$form['target_bundles'] = array(
'#type' => 'value',
'#value' => array(),
);
}
$form['sort']['type'] = array(
'#type' => 'select',
'#title' => t('Sort by'),
'#options' => array(
'none' => t("Don't sort"),
'property' => t('A property of the base table of the entity'),
'field' => t('A field attached to this entity'),
),
'#ajax' => TRUE,
'#limit_validation_errors' => array(),
'#default_value' => $field['settings']['handler_settings']['sort']['type'],
);
$form['sort']['settings'] = array(
'#type' => 'container',
'#attributes' => array('class' => array('entityreference-settings')),
'#process' => array('_entityreference_form_process_merge_parent'),
);
if ($field['settings']['handler_settings']['sort']['type'] == 'property') {
// Merge-in default values.
$field['settings']['handler_settings']['sort'] += array(
'property' => NULL,
);
$form['sort']['settings']['property'] = array(
'#type' => 'select',
'#title' => t('Sort property'),
'#required' => TRUE,
'#options' => drupal_map_assoc($entity_info['schema_fields_sql']['base table']),
'#default_value' => $field['settings']['handler_settings']['sort']['property'],
);
}
elseif ($field['settings']['handler_settings']['sort']['type'] == 'field') {
// Merge-in default values.
$field['settings']['handler_settings']['sort'] += array(
'field' => NULL,
);
$fields = array();
foreach (field_info_instances($field['settings']['target_type']) as $bundle_name => $bundle_instances) {
foreach ($bundle_instances as $instance_name => $instance_info) {
$field_info = field_info_field($instance_name);
foreach ($field_info['columns'] as $column_name => $column_info) {
$fields[$instance_name . ':' . $column_name] = t('@label (column @column)', array('@label' => $instance_info['label'], '@column' => $column_name));
}
}
}
$form['sort']['settings']['field'] = array(
'#type' => 'select',
'#title' => t('Sort field'),
'#required' => TRUE,
'#options' => $fields,
'#default_value' => $field['settings']['handler_settings']['sort']['field'],
);
}
if ($field['settings']['handler_settings']['sort']['type'] != 'none') {
// Merge-in default values.
$field['settings']['handler_settings']['sort'] += array(
'direction' => 'ASC',
);
$form['sort']['settings']['direction'] = array(
'#type' => 'select',
'#title' => t('Sort direction'),
'#required' => TRUE,
'#options' => array(
'ASC' => t('Ascending'),
'DESC' => t('Descending'),
),
'#default_value' => $field['settings']['handler_settings']['sort']['direction'],
);
}
return $form;
}
/**
* Implements EntityReferenceHandler::getReferencableEntities().
*/
public function getReferencableEntities($match = NULL, $match_operator = 'CONTAINS', $limit = 0) {
$options = array();
$entity_type = $this->field['settings']['target_type'];
$query = $this->buildEntityFieldQuery($match, $match_operator);
if ($limit > 0) {
$query->range(0, $limit);
}
$results = $query->execute();
if (!empty($results[$entity_type])) {
$entities = entity_load($entity_type, array_keys($results[$entity_type]));
foreach ($entities as $entity_id => $entity) {
list(,, $bundle) = entity_extract_ids($entity_type, $entity);
$options[$bundle][$entity_id] = check_plain($this->getLabel($entity));
}
}
return $options;
}
/**
* Implements EntityReferenceHandler::countReferencableEntities().
*/
public function countReferencableEntities($match = NULL, $match_operator = 'CONTAINS') {
$query = $this->buildEntityFieldQuery($match, $match_operator);
return $query
->count()
->execute();
}
/**
* Implements EntityReferenceHandler::validateReferencableEntities().
*/
public function validateReferencableEntities(array $ids) {
if ($ids) {
$entity_type = $this->field['settings']['target_type'];
$query = $this->buildEntityFieldQuery();
$query->entityCondition('entity_id', $ids, 'IN');
$result = $query->execute();
if (!empty($result[$entity_type])) {
return array_keys($result[$entity_type]);
}
}
return array();
}
/**
* Implements EntityReferenceHandler::validateAutocompleteInput().
*/
public function validateAutocompleteInput($input, &$element, &$form_state, $form) {
$entities = $this->getReferencableEntities($input, '=', 6);
if (empty($entities)) {
// Error if there are no entities available for a required field.
form_error($element, t('There are no entities matching "%value"', array('%value' => $input)));
}
elseif (count($entities) > 5) {
// Error if there are more than 5 matching entities.
form_error($element, t('Many entities are called %value. Specify the one you want by appending the id in parentheses, like "@value (@id)"', array(
'%value' => $input,
'@value' => $input,
'@id' => key($entities),
)));
}
elseif (count($entities) > 1) {
// More helpful error if there are only a few matching entities.
$multiples = array();
foreach ($entities as $id => $name) {
$multiples[] = $name . ' (' . $id . ')';
}
form_error($element, t('Multiple entities match this reference; "%multiple"', array('%multiple' => implode('", "', $multiples))));
}
else {
// Take the one and only matching entity.
return key($entities);
}
}
/**
* Build an EntityFieldQuery to get referencable entities.
*/
protected function buildEntityFieldQuery($match = NULL, $match_operator = 'CONTAINS') {
$query = new EntityFieldQuery();
$query->entityCondition('entity_type', $this->field['settings']['target_type']);
if (!empty($this->field['settings']['handler_settings']['target_bundles'])) {
$query->entityCondition('bundle', $this->field['settings']['handler_settings']['target_bundles'], 'IN');
}
if (isset($match)) {
$entity_info = entity_get_info($this->field['settings']['target_type']);
if (isset($entity_info['entity keys']['label'])) {
$query->propertyCondition($entity_info['entity keys']['label'], $match, $match_operator);
}
}
// Add a generic entity access tag to the query.
$query->addTag($this->field['settings']['target_type'] . '_access');
$query->addTag('entityreference');
$query->addMetaData('field', $this->field);
$query->addMetaData('entityreference_selection_handler', $this);
// Add the sort option.
if (!empty($this->field['settings']['handler_settings']['sort'])) {
$sort_settings = $this->field['settings']['handler_settings']['sort'];
if ($sort_settings['type'] == 'property') {
$query->propertyOrderBy($sort_settings['property'], $sort_settings['direction']);
}
elseif ($sort_settings['type'] == 'field') {
list($field, $column) = explode(':', $sort_settings['field'], 2);
$query->fieldOrderBy($field, $column, $sort_settings['direction']);
}
}
return $query;
}
/**
* Implements EntityReferenceHandler::entityFieldQueryAlter().
*/
public function entityFieldQueryAlter(SelectQueryInterface $query) {
}
/**
* Helper method: pass a query to the alteration system again.
*
* This allow Entity Reference to add a tag to an existing query, to ask
* access control mechanisms to alter it again.
*/
protected function reAlterQuery(SelectQueryInterface $query, $tag, $base_table) {
// Save the old tags and metadata.
// For some reason, those are public.
$old_tags = $query->alterTags;
$old_metadata = $query->alterMetaData;
$query->alterTags = array($tag => TRUE);
$query->alterMetaData['base_table'] = $base_table;
drupal_alter(array('query', 'query_' . $tag), $query);
// Restore the tags and metadata.
$query->alterTags = $old_tags;
$query->alterMetaData = $old_metadata;
}
/**
* Implements EntityReferenceHandler::getLabel().
*/
public function getLabel($entity) {
return entity_label($this->field['settings']['target_type'], $entity);
}
/**
* Ensure a base table exists for the query.
*
* If we have a field-only query, we want to assure we have a base-table
* so we can later alter the query in entityFieldQueryAlter().
*
* @param $query
* The Select query.
*
* @return
* The alias of the base-table.
*/
public function ensureBaseTable(SelectQueryInterface $query) {
$tables = $query->getTables();
// Check the current base table.
foreach ($tables as $table) {
if (empty($table['join'])) {
$alias = $table['alias'];
break;
}
}
if (strpos($alias, 'field_data_') !== 0) {
// The existing base-table is the correct one.
return $alias;
}
// Join the known base-table.
$target_type = $this->field['settings']['target_type'];
$entity_info = entity_get_info($target_type);
$id = $entity_info['entity keys']['id'];
// Return the alias of the table.
return $query->innerJoin($target_type, NULL, "$target_type.$id = $alias.entity_id");
}
}
/**
* Override for the Node type.
*
* This only exists to workaround core bugs.
*/
class EntityReference_SelectionHandler_Generic_node extends EntityReference_SelectionHandler_Generic {
public function entityFieldQueryAlter(SelectQueryInterface $query) {
// Adding the 'node_access' tag is sadly insufficient for nodes: core
// requires us to also know about the concept of 'published' and
// 'unpublished'. We need to do that as long as there are no access control
// modules in use on the site. As long as one access control module is there,
// it is supposed to handle this check.
if (!user_access('bypass node access') && !count(module_implements('node_grants'))) {
$base_table = $this->ensureBaseTable($query);
$query->condition("$base_table.status", NODE_PUBLISHED);
}
}
}
/**
* Override for the User type.
*
* This only exists to workaround core bugs.
*/
class EntityReference_SelectionHandler_Generic_user extends EntityReference_SelectionHandler_Generic {
public function buildEntityFieldQuery($match = NULL, $match_operator = 'CONTAINS') {
$query = parent::buildEntityFieldQuery($match, $match_operator);
// The user entity doesn't have a label column.
if (isset($match)) {
$query->propertyCondition('name', $match, $match_operator);
}
// Adding the 'user_access' tag is sadly insufficient for users: core
// requires us to also know about the concept of 'blocked' and
// 'active'.
if (!user_access('administer users')) {
$query->propertyCondition('status', 1);
}
return $query;
}
public function entityFieldQueryAlter(SelectQueryInterface $query) {
if (user_access('administer users')) {
// In addition, if the user is administrator, we need to make sure to
// match the anonymous user, that doesn't actually have a name in the
// database.
$conditions = &$query->conditions();
foreach ($conditions as $key => $condition) {
if ($key !== '#conjunction' && is_string($condition['field']) && $condition['field'] === 'users.name') {
// Remove the condition.
unset($conditions[$key]);
// Re-add the condition and a condition on uid = 0 so that we end up
// with a query in the form:
// WHERE (name LIKE :name) OR (:anonymous_name LIKE :name AND uid = 0)
$or = db_or();
$or->condition($condition['field'], $condition['value'], $condition['operator']);
// Sadly, the Database layer doesn't allow us to build a condition
// in the form ':placeholder = :placeholder2', because the 'field'
// part of a condition is always escaped.
// As a (cheap) workaround, we separately build a condition with no
// field, and concatenate the field and the condition separately.
$value_part = db_and();
$value_part->condition('anonymous_name', $condition['value'], $condition['operator']);
$value_part->compile(Database::getConnection(), $query);
$or->condition(db_and()
->where(str_replace('anonymous_name', ':anonymous_name', (string) $value_part), $value_part->arguments() + array(':anonymous_name' => format_username(user_load(0))))
->condition('users.uid', 0)
);
$query->condition($or);
}
}
}
}
}
/**
* Override for the Comment type.
*
* This only exists to workaround core bugs.
*/
class EntityReference_SelectionHandler_Generic_comment extends EntityReference_SelectionHandler_Generic {
public function entityFieldQueryAlter(SelectQueryInterface $query) {
// Adding the 'comment_access' tag is sadly insufficient for comments: core
// requires us to also know about the concept of 'published' and
// 'unpublished'.
if (!user_access('administer comments')) {
$base_table = $this->ensureBaseTable($query);
$query->condition("$base_table.status", COMMENT_PUBLISHED);
}
// The Comment module doesn't implement any proper comment access,
// and as a consequence doesn't make sure that comments cannot be viewed
// when the user doesn't have access to the node.
$tables = $query->getTables();
$base_table = key($tables);
$node_alias = $query->innerJoin('node', 'n', '%alias.nid = ' . $base_table . '.nid');
// Pass the query to the node access control.
$this->reAlterQuery($query, 'node_access', $node_alias);
// Alas, the comment entity exposes a bundle, but doesn't have a bundle column
// in the database. We have to alter the query ourself to go fetch the
// bundle.
$conditions = &$query->conditions();
foreach ($conditions as $key => &$condition) {
if ($key !== '#conjunction' && is_string($condition['field']) && $condition['field'] === 'node_type') {
$condition['field'] = $node_alias . '.type';
foreach ($condition['value'] as &$value) {
if (substr($value, 0, 13) == 'comment_node_') {
$value = substr($value, 13);
}
}
break;
}
}
// Passing the query to node_query_node_access_alter() is sadly
// insufficient for nodes.
// @see EntityReferenceHandler_node::entityFieldQueryAlter()
if (!user_access('bypass node access') && !count(module_implements('node_grants'))) {
$query->condition($node_alias . '.status', 1);
}
}
}
/**
* Override for the File type.
*
* This only exists to workaround core bugs.
*/
class EntityReference_SelectionHandler_Generic_file extends EntityReference_SelectionHandler_Generic {
public function entityFieldQueryAlter(SelectQueryInterface $query) {
// Core forces us to know about 'permanent' vs. 'temporary' files.
$tables = $query->getTables();
$base_table = key($tables);
$query->condition('status', FILE_STATUS_PERMANENT);
// Access control to files is a very difficult business. For now, we are not
// going to give it a shot.
// @todo: fix this when core access control is less insane.
return $query;
}
public function getLabel($entity) {
// The file entity doesn't have a label. More over, the filename is
// sometimes empty, so use the basename in that case.
return $entity->filename !== '' ? $entity->filename : basename($entity->uri);
}
}
/**
* Override for the Taxonomy term type.
*
* This only exists to workaround core bugs.
*/
class EntityReference_SelectionHandler_Generic_taxonomy_term extends EntityReference_SelectionHandler_Generic {
public function entityFieldQueryAlter(SelectQueryInterface $query) {
// The Taxonomy module doesn't implement any proper taxonomy term access,
// and as a consequence doesn't make sure that taxonomy terms cannot be viewed
// when the user doesn't have access to the vocabulary.
$base_table = $this->ensureBaseTable($query);
$vocabulary_alias = $query->innerJoin('taxonomy_vocabulary', 'n', '%alias.vid = ' . $base_table . '.vid');
$query->addMetadata('base_table', $vocabulary_alias);
// Pass the query to the taxonomy access control.
$this->reAlterQuery($query, 'taxonomy_vocabulary_access', $vocabulary_alias);
// Also, the taxonomy term entity exposes a bundle, but doesn't have a bundle
// column in the database. We have to alter the query ourself to go fetch
// the bundle.
$conditions = &$query->conditions();
foreach ($conditions as $key => &$condition) {
if ($key !== '#conjunction' && is_string($condition['field']) && $condition['field'] === 'vocabulary_machine_name') {
$condition['field'] = $vocabulary_alias . '.machine_name';
break;
}
}
}
/**
* Implements EntityReferenceHandler::getReferencableEntities().
*/
public function getReferencableEntities($match = NULL, $match_operator = 'CONTAINS', $limit = 0) {
if ($match || $limit) {
return parent::getReferencableEntities($match , $match_operator, $limit);
}
$options = array();
$entity_type = $this->field['settings']['target_type'];
// We imitate core by calling taxonomy_get_tree().
$entity_info = entity_get_info('taxonomy_term');
$bundles = !empty($this->field['settings']['handler_settings']['target_bundles']) ? $this->field['settings']['handler_settings']['target_bundles'] : array_keys($entity_info['bundles']);
foreach ($bundles as $bundle) {
if ($vocabulary = taxonomy_vocabulary_machine_name_load($bundle)) {
if ($terms = taxonomy_get_tree($vocabulary->vid, 0)) {
foreach ($terms as $term) {
$options[$vocabulary->machine_name][$term->tid] = str_repeat('-', $term->depth) . check_plain($term->name);
}
}
}
}
return $options;
}
}

View File

@@ -0,0 +1,248 @@
<?php
/**
* @file
* Test case for simple CCK field mapper mappers/content.inc.
*/
/**
* Class for testing Feeds field mapper.
*/
class FeedsMapperFieldTestCase extends DrupalWebTestCase{
/**
* Test info function.
*/
public static function getInfo() {
return array(
'name' => 'Feeds integration (field mapper)',
'description' => 'Test Feeds Mapper support for fields.',
'group' => 'Entity Reference',
);
}
/**
* Set-up function.
*/
public function setUp() {
parent::setUp();
module_enable(array('entityreference_feeds_test'), TRUE);
$this->resetAll();
if (!module_exists('feeds')) {
return;
}
$permissions[] = 'access content';
$permissions[] = 'administer site configuration';
$permissions[] = 'administer content types';
$permissions[] = 'administer nodes';
$permissions[] = 'bypass node access';
$permissions[] = 'administer taxonomy';
$permissions[] = 'administer users';
$permissions[] = 'administer feeds';
// Create an admin user and log in.
$this->admin_user = $this->drupalCreateUser($permissions);
$this->drupalLogin($this->admin_user);
}
/**
* Check if mapping exists.
*
* @param string $id
* ID of the importer.
* @param integer $i
* The key of the mapping.
* @param string $source
* The source field.
* @param string $target
* The target field.
*
* @return integer
* -1 if the mapping doesn't exist, the key of the mapping otherwise.
*/
public function mappingExists($id, $i, $source, $target) {
$current_mappings = $this->getCurrentMappings($id);
if ($current_mappings) {
foreach ($current_mappings as $key => $mapping) {
if ($mapping['source'] == $source && $mapping['target'] == $target && $key == $i) {
return $key;
}
}
}
return -1;
}
/**
* Adds mappings to a given configuration.
*
* @param string $id
* ID of the importer.
* @param array $mappings
* An array of mapping arrays. Each mapping array must have a source and
* an target key and can have a unique key.
* @param bool $test_mappings
* (optional) TRUE to automatically test mapping configs. Defaults to TRUE.
*/
public function addMappings($id, $mappings, $test_mappings = TRUE) {
$path = "admin/structure/feeds/$id/mapping";
// Iterate through all mappings and add the mapping via the form.
foreach ($mappings as $i => $mapping) {
if ($test_mappings) {
$current_mapping_key = $this->mappingExists($id, $i, $mapping['source'], $mapping['target']);
$this->assertEqual($current_mapping_key, -1, 'Mapping does not exist before addition.');
}
// Get unique flag and unset it. Otherwise, drupalPost will complain that
// Split up config and mapping.
$config = $mapping;
unset($config['source'], $config['target']);
$mapping = array('source' => $mapping['source'], 'target' => $mapping['target']);
// Add mapping.
$this->drupalPost($path, $mapping, t('Add'));
// If there are other configuration options, set them.
if ($config) {
$this->drupalPostAJAX(NULL, array(), 'mapping_settings_edit_' . $i);
// Set some settings.
$edit = array();
foreach ($config as $key => $value) {
$edit["config[$i][settings][$key]"] = $value;
}
$this->drupalPostAJAX(NULL, $edit, 'mapping_settings_update_' . $i);
$this->drupalPost(NULL, array(), t('Save'));
}
if ($test_mappings) {
$current_mapping_key = $this->mappingExists($id, $i, $mapping['source'], $mapping['target']);
$this->assertTrue($current_mapping_key >= 0, 'Mapping exists after addition.');
}
}
}
/**
* Gets an array of current mappings from the feeds_importer config.
*
* @param string $id
* ID of the importer.
*
* @return bool|array
* FALSE if the importer has no mappings, or an an array of mappings.
*/
public function getCurrentMappings($id) {
$config = db_query("SELECT config FROM {feeds_importer} WHERE id = :id", array(':id' => $id))->fetchField();
$config = unserialize($config);
// We are very specific here. 'mappings' can either be an array or not
// exist.
if (array_key_exists('mappings', $config['processor']['config'])) {
$this->assertTrue(is_array($config['processor']['config']['mappings']), 'Mappings is an array.');
return $config['processor']['config']['mappings'];
}
return FALSE;
}
/**
* Basic test loading a double entry CSV file.
*/
public function test() {
if (!module_exists('feeds')) {
return;
}
$this->drupalLogin($this->admin_user);
$this->drupalGet('admin/structure/types/manage/article/fields');
$this->assertText('Ref - entity ID', t('Found Entity reference field %field.', array('%field' => 'field_er_id')));
$this->assertText('Ref - entity label', t('Found Entity reference field %field.', array('%field' => 'field_er_label')));
$this->assertText('Ref - feeds GUID', t('Found Entity reference field %field.', array('%field' => 'field_er_guid')));
$this->assertText('Ref - feeds URL', t('Found Entity reference field %field.', array('%field' => 'field_er_url')));
// Add feeds importer
$this->drupalGet('admin/structure/feeds');
$this->clickLink('Add importer');
$this->drupalPost('admin/structure/feeds/create', array('name' => 'Nodes', 'id' => 'nodes'), 'Create');
$this->assertText('Your configuration has been created with default settings.');
$this->drupalPost('admin/structure/feeds/nodes/settings/', array('content_type' => '', 'import_period' => -1), 'Save');
$this->assertText('Your changes have been saved.');
$this->drupalPost("admin/structure/feeds/nodes/fetcher", array('plugin_key' => 'FeedsFileFetcher'), 'Save');
$config = unserialize(db_query("SELECT config FROM {feeds_importer} WHERE id = :id", array(':id' => 'nodes'))->fetchField());
$this->assertEqual($config['fetcher']['plugin_key'], 'FeedsFileFetcher', 'Verified correct fetcher (FeedsFileFetcher).');
$this->drupalPost("admin/structure/feeds/nodes/parser", array('plugin_key' => 'FeedsCSVParser'), 'Save');
$config = unserialize(db_query("SELECT config FROM {feeds_importer} WHERE id = :id", array(':id' => 'nodes'))->fetchField());
$this->assertEqual($config['parser']['plugin_key'], 'FeedsCSVParser', 'Verified correct parser (FeedsCSVParser).');
$this->drupalPost('admin/structure/feeds/nodes/settings/FeedsNodeProcessor', array('content_type' => 'article'), 'Save');
$this->assertText('Your changes have been saved.');
$this->addMappings('nodes', array(
0 => array(
'source' => 'title',
'target' => 'title',
),
1 => array(
'source' => 'nid',
'target' => 'nid',
'unique' => TRUE,
),
2 => array(
'source' => 'permalink',
'target' => 'url',
'unique' => TRUE,
),
3 => array(
'source' => 'nid',
'target' => 'guid',
'unique' => TRUE,
),
4 => array(
'source' => 'parent_nid',
'target' => 'field_er_id:etid',
),
5 => array(
'source' => 'parent_title',
'target' => 'field_er_label:label',
),
6 => array(
'source' => 'parent_url',
'target' => 'field_er_url:url',
),
7 => array(
'source' => 'parent_guid',
'target' => 'field_er_guid',
),
)
);
$file = realpath(getcwd()) . '/' . drupal_get_path('module', 'entityreference') . '/tests/feeds_test.csv';
$this->assertTrue(file_exists($file), 'Source file exists');
$this->drupalPost('import/nodes', array('files[feeds]' => $file), 'Import');
$this->assertText('Created 2 nodes');
$parent = node_load(1);
$this->assertTrue(empty($parent->field_er_id['und'][0]['target_id']), t('Parent node: Import by entity ID OK.'));
$this->assertTrue(empty($parent->field_er_label['und'][0]['target_id']), t('Parent node: Import by entity label OK.'));
$this->assertTrue(empty($parent->field_er_guid['und'][0]['target_id']), t('Parent node: Import by feeds GUID OK.'));
$this->assertTrue(empty($parent->field_er_url['und'][0]['target_id']), t('Parent node: Import by feeds URL OK.'));
$child = node_load(2);
$this->assertTrue($child->field_er_id['und'][0]['target_id'] == 1, t('Child node: Import by entity ID OK.'));
$this->assertTrue($child->field_er_label['und'][0]['target_id'] == 1, t('Child node: Import by entity label OK.'));
$this->assertTrue($child->field_er_guid['und'][0]['target_id'] == 1, t('Child node: Import by feeds GUID OK.'));
$this->assertTrue($child->field_er_url['und'][0]['target_id'] == 1, t('Child node: Import by feeds URL OK.'));
}
}

View File

@@ -84,6 +84,14 @@ class EntityReferenceHandlersTestCase extends DrupalWebTestCase {
'title' => 'Node unpublished (<&>)',
'uid' => 1,
),
// Title purposefully starts with same characters as published1 and
// published2 above but contains a slash.
'published_withslash' => (object) array(
'type' => 'article',
'status' => 1,
'title' => 'Node pub/lished1',
'uid' => 1,
),
);
$node_labels = array();
@@ -104,6 +112,7 @@ class EntityReferenceHandlersTestCase extends DrupalWebTestCase {
'article' => array(
$nodes['published1']->nid => $node_labels['published1'],
$nodes['published2']->nid => $node_labels['published2'],
$nodes['published_withslash']->nid => $node_labels['published_withslash'],
),
),
),
@@ -141,6 +150,18 @@ class EntityReferenceHandlersTestCase extends DrupalWebTestCase {
),
'result' => array(),
),
// Searching for "Node pub/" should return only the published_withslash node
// and not published1 and published2 from above.
array(
'arguments' => array(
array('Node pub/', 'CONTAINS'),
),
'result' => array(
'article' => array(
$nodes['published_withslash']->nid => $node_labels['published_withslash'],
),
),
),
);
$this->assertReferencable($field, $referencable_tests, 'Node handler');
@@ -156,6 +177,7 @@ class EntityReferenceHandlersTestCase extends DrupalWebTestCase {
'article' => array(
$nodes['published1']->nid => $node_labels['published1'],
$nodes['published2']->nid => $node_labels['published2'],
$nodes['published_withslash']->nid => $node_labels['published_withslash'],
$nodes['unpublished']->nid => $node_labels['unpublished'],
),
),
@@ -234,7 +256,7 @@ class EntityReferenceHandlersTestCase extends DrupalWebTestCase {
),
'result' => array(
'user' => array(
$users['admin']->uid => $user_labels['admin'],
$users['admin']->uid => '- Restricted access -',
$users['non_admin']->uid => $user_labels['non_admin'],
),
),

View File

@@ -0,0 +1,3 @@
"nid","title","permalink","parent_nid","parent_title","parent_url","parent_guid"
"1","Views","http://drupal.org/project/views","","","",""
"2","Views bulk operations","http://drupal.org/project/views_bulk_operations","1","Views","http://drupal.org/project/views","1"
1 nid title permalink parent_nid parent_title parent_url parent_guid
2 1 Views http://drupal.org/project/views
3 2 Views bulk operations http://drupal.org/project/views_bulk_operations 1 Views http://drupal.org/project/views 1

View File

@@ -0,0 +1,16 @@
name = "Entityreference - Feeds integration tests"
description = "Support module for the Entityreference - Feeds integration tests."
package = Testing
core = 7.x
hidden = TRUE
dependencies[] = feeds
dependencies[] = feeds_ui
dependencies[] = entityreference
; Information added by packaging script on 2013-11-20
version = "7.x-1.1"
core = "7.x"
project = "entityreference"
datestamp = "1384973110"

View File

@@ -0,0 +1,104 @@
<?php
/**
* @file
* Support module for Entity reference - Feeds integration.
*/
/**
* Implements hook_install().
*/
function entityreference_feeds_test_install() {
field_info_cache_clear();
// Entity reference field - mapped by ID.
$field = array(
'field_name' => 'field_er_id',
'type' => 'entityreference',
'cardinality' => 1,
'settings' => array(
'target_type' => 'node',
'handler' => 'base',
'handler_settings' => array(
'target_bundles' => array('article' => 'article'),
),
),
);
field_create_field($field);
$instance = array(
'field_name' => 'field_er_id',
'entity_type' => 'node',
'label' => 'Ref - entity ID',
'bundle' => 'article',
);
field_create_instance($instance);
// Entity reference field - mapped by Feeds GUID.
$field = array(
'field_name' => 'field_er_guid',
'type' => 'entityreference',
'cardinality' => 1,
'settings' => array(
'target_type' => 'node',
'handler' => 'base',
'handler_settings' => array(
'target_bundles' => array('article' => 'article'),
),
),
);
field_create_field($field);
$instance = array(
'field_name' => 'field_er_guid',
'entity_type' => 'node',
'label' => 'Ref - feeds GUID',
'bundle' => 'article',
);
field_create_instance($instance);
// Entity reference field - mapped by Feeds URL.
$field = array(
'field_name' => 'field_er_url',
'type' => 'entityreference',
'cardinality' => 1,
'settings' => array(
'target_type' => 'node',
'handler' => 'base',
'handler_settings' => array(
'target_bundles' => array('article' => 'article'),
),
),
);
field_create_field($field);
$instance = array(
'field_name' => 'field_er_url',
'entity_type' => 'node',
'label' => 'Ref - feeds URL',
'bundle' => 'article',
);
field_create_instance($instance);
// Entity reference field - mapped by Label.
$field = array(
'field_name' => 'field_er_label',
'type' => 'entityreference',
'cardinality' => 1,
'settings' => array(
'target_type' => 'node',
'handler' => 'base',
'handler_settings' => array(
'target_bundles' => array('article' => 'article'),
),
),
);
field_create_field($field);
$instance = array(
'field_name' => 'field_er_label',
'entity_type' => 'node',
'label' => 'Ref - entity label',
'bundle' => 'article',
);
field_create_instance($instance);
}

View File

@@ -80,6 +80,17 @@ function entityreference_field_views_data_views_data_alter(&$data, $field) {
'group' => t('Entity Reference'),
'title' => t('Referencing entity'),
'help' => t('A bridge to the @entity entity that is referencing @target_entity via !field_name', $replacements),
'join_extra' => array(
0 => array(
'field' => 'entity_type',
'value' => $entity_type,
),
1 => array(
'field' => 'deleted',
'value' => 0,
'numeric' => TRUE,
),
),
);
}
}

View File

@@ -75,9 +75,11 @@ class entityreference_plugin_display extends views_plugin_display {
// Build the condition using the selected search fields
foreach ($style_options['search_fields'] as $field_alias) {
if (!empty($field_alias)) {
// Get the table and field names for the checked field
if (empty($this->view->field[$field_alias]->field_info))
// Get the table and field names for the checked field.
if (empty($this->view->field[$field_alias]->field_info)) {
$field = $this->view->query->fields[$this->view->field[$field_alias]->field_alias];
}
else {
$this->view->query->add_field($this->view->field[$field_alias]->options['table'], $this->view->field[$field_alias]->real_field, $this->view->field[$field_alias]->options['field'], array());
$field = $this->view->query->fields[$this->view->field[$field_alias]->options['field']];

View File

@@ -45,19 +45,17 @@ class entityreference_plugin_style extends views_plugin_style {
// Group the rows according to the grouping field, if specified.
$sets = $this->render_grouping($this->view->result, $this->options['grouping']);
// Grab the alias of the 'id' field added by entityreference_plugin_display.
$id_field_alias = $this->display->handler->id_field_alias;
// @todo We don't display grouping info for now. Could be useful for select
// widget, though.
$results = array();
$this->view->row_index = 0;
foreach ($sets as $records) {
foreach ($records as $values) {
foreach ($records as $index => $values) {
$this->view->row_index = $index;
// Sanitize html, remove line breaks and extra whitespace.
$results[$values->{$id_field_alias}] = filter_xss_admin(preg_replace('/\s\s+/', ' ', str_replace("\n", '', $this->row_plugin->render($values))));
$this->view->row_index++;
}
}
unset($this->view->row_index);