contrib modules security updates

This commit is contained in:
Bachir Soussi Chiadmi
2016-10-13 12:10:40 +02:00
parent ffd758abc9
commit 747127f643
732 changed files with 67976 additions and 23207 deletions

View File

@@ -6,13 +6,13 @@
*/
/**
* Implements hook_feeds_processor_targets_alter().
*
* @see FeedsNodeProcessor::getMappingTargets().
* Implements hook_feeds_processor_targets().
*
* @todo Only provides "end date" target if field allows it.
*/
function date_feeds_processor_targets_alter(&$targets, $entity_type, $bundle_name) {
function date_feeds_processor_targets($entity_type, $bundle_name) {
$targets = array();
foreach (field_info_instances($entity_type, $bundle_name) as $name => $instance) {
$info = field_info_field($name);
if (in_array($info['type'], array('date', 'datestamp', 'datetime'))) {
@@ -30,33 +30,33 @@ function date_feeds_processor_targets_alter(&$targets, $entity_type, $bundle_nam
);
}
}
return $targets;
}
/**
* Implements hook_feeds_set_target().
*
* @param $node
* The target node.
* @param $field_name
* The name of field on the target node to map to.
* @param $feed_element
* The value to be mapped. Should be either a (flexible) date string
* or a FeedsDateTimeElement object.
*
* @todo Support array of values for dates.
* Callback for setting date values.
*/
function date_feeds_set_target($source, $entity, $target, $feed_element) {
function date_feeds_set_target(FeedsSource $source, $entity, $target, array $values, array $mapping) {
list($field_name, $sub_field) = explode(':', $target, 2);
if (!($feed_element instanceof FeedsDateTimeElement)) {
if (is_array($feed_element)) {
$feed_element = $feed_element[0];
}
if ($sub_field == 'end') {
$feed_element = new FeedsDateTimeElement(NULL, $feed_element);
}
else {
$feed_element = new FeedsDateTimeElement($feed_element, NULL);
$delta = 0;
foreach ($values as $value) {
if (!($value instanceof FeedsDateTimeElement)) {
if (empty($value) || !is_numeric($value) && is_string($value) && !date_create($value)) {
$value = new FeedsDateTimeElement(NULL, NULL);
}
elseif ($sub_field == 'end') {
$value = new FeedsDateTimeElement(NULL, $value);
}
else {
$value = new FeedsDateTimeElement($value, NULL);
}
}
$value->buildDateField($entity, $field_name, $delta, $mapping['language']);
$delta++;
}
$feed_element->buildDateField($entity, $field_name);
}

View File

@@ -0,0 +1,83 @@
<?php
/**
* @file
* On behalf implementation of Feeds mapping API for entity_translation.module.
*/
/**
* Implements hook_feeds_presave().
*/
function entity_translation_feeds_presave(FeedsSource $source, $entity, $item, $entity_id) {
$entity_type = $entity->feeds_item->entity_type;
// Check that it's a real entity type, and translation is enabled.
if (!entity_get_info($entity_type) || !entity_translation_enabled($entity_type, $entity)) {
return;
}
if (!$handler = entity_translation_get_handler($entity_type, $entity)) {
return;
}
list(, , $bundle) = entity_extract_ids($entity_type, $entity);
$languages_seen = array();
foreach (field_info_instances($entity_type, $bundle) as $instance) {
$field_name = $instance['field_name'];
// No values in this field, skip it.
if (empty($entity->$field_name) || !is_array($entity->$field_name)) {
continue;
}
// Not translatable.
$info = field_info_field($field_name);
if (!$info || !$info['translatable']) {
continue;
}
// Init the translation handler.
if (empty($handler->getTranslations()->original)) {
$handler->initTranslations();
}
// Avoid invalid user configuration. Entity translation does this when
// loading the translation overview page.
if (count($entity->$field_name) === 1 && key($entity->$field_name) === LANGUAGE_NONE && $handler->getLanguage() !== LANGUAGE_NONE) {
$entity->{$field_name}[$handler->getLanguage()] = $entity->{$field_name}[LANGUAGE_NONE];
$entity->{$field_name}[LANGUAGE_NONE] = array();
}
// Look for languages we haven't created a translation for yet.
foreach (array_diff_key($entity->$field_name, $languages_seen) as $language => $v) {
if ($language === LANGUAGE_NONE) {
continue;
}
$languages_seen[$language] = TRUE;
if ($language === $handler->getLanguage()) {
continue;
}
$translation = array(
'translate' => 0,
'status' => 1,
'language' => $language,
'source' => $handler->getLanguage(),
);
$handler->setTranslation($translation, $entity);
}
}
// Loop through every language for the site, and remove translations for the
// ones that don't have any values.
foreach (language_list() as $language) {
if (!isset($languages_seen[$language->language])) {
$handler->removeTranslation($language->language);
}
}
}

View File

@@ -0,0 +1,50 @@
<?php
/**
* @file
* On behalf implementation of Feeds mapping API for field.module.
*/
/**
* Implements hook_feeds_presave().
*/
function field_feeds_presave(FeedsSource $source, $entity, $item, $entity_id) {
$entity_type = $entity->feeds_item->entity_type;
// Not a real entity.
if (!entity_get_info($entity_type)) {
return;
}
// Gather the fields that Feeds is mapping to.
$feeds_fields = array();
foreach ($source->importer()->processor->getMappings() as $mapping) {
list($field) = explode(':', $mapping['target']);
$feeds_fields[$field] = TRUE;
}
list(, , $bundle) = entity_extract_ids($entity_type, $entity);
foreach (field_info_instances($entity_type, $bundle) as $instance) {
$field_name = $instance['field_name'];
// Skip fields that Feeds isn't mapping to, and empty fields.
if (!isset($feeds_fields[$field_name]) || empty($entity->$field_name) || !is_array($entity->$field_name)) {
continue;
}
$info = field_info_field($field_name);
foreach ($entity->$field_name as $language => $values) {
// Filter out empty values.
$values = _field_filter_items($info, $values);
// Check that the number of values doesn't exceed the field cardinality.
if ($info['cardinality'] != FIELD_CARDINALITY_UNLIMITED && count($values) > $info['cardinality']) {
$values = array_slice($values, 0, $info['cardinality']);
}
$entity->{$field_name}[$language] = $values;
}
}
}

View File

@@ -4,91 +4,139 @@
* @file
* On behalf implementation of Feeds mapping API for file.module and
* image.module.
*
* Does actually not include mappers for field types defined in fields module
* (because there aren't any) but mappers for all fields that contain their
* value simply in $entity->fieldname['und'][$i]['value'].
*/
/**
* Implements hook_feeds_processor_targets_alter().
*
* @see FeedsNodeProcessor::getMappingTargets().
* Implements hook_feeds_processor_targets().
*/
function file_feeds_processor_targets_alter(&$targets, $entity_type, $bundle_name) {
function file_feeds_processor_targets($entity_type, $bundle_name) {
$targets = array();
foreach (field_info_instances($entity_type, $bundle_name) as $name => $instance) {
$info = field_info_field($name);
if (in_array($info['type'], array('file', 'image'))) {
$targets[$name] = array(
'name' => check_plain($instance['label']),
$targets[$name . ':uri'] = array(
'name' => t('@label: URI', array('@label' => $instance['label'])),
'callback' => 'file_feeds_set_target',
'description' => t('The @label field of the node.', array('@label' => $instance['label'])),
'description' => t('The URI of the @label field.', array('@label' => $instance['label'])),
'real_target' => $name,
);
// Keep the old target name for backwards compatibility, but hide it from
// the UI.
$targets[$name] = $targets[$name . ':uri'];
$targets[$name]['deprecated'] = TRUE;
if ($info['type'] == 'image') {
$targets[$name . ':alt'] = array(
'name' => t('@label: Alt', array('@label' => $instance['label'])),
'callback' => 'file_feeds_set_target',
'description' => t('The alt tag of the @label field.', array('@label' => $instance['label'])),
'real_target' => $name,
);
$targets[$name . ':title'] = array(
'name' => t('@label: Title', array('@label' => $instance['label'])),
'callback' => 'file_feeds_set_target',
'description' => t('The title of the @label field.', array('@label' => $instance['label'])),
'real_target' => $name,
);
}
elseif ($info['type'] === 'file') {
$targets[$name . ':description'] = array(
'name' => t('@label: Description', array('@label' => $instance['label'])),
'callback' => 'file_feeds_set_target',
'description' => t('The description of the @label field.', array('@label' => $instance['label'])),
'real_target' => $name,
);
}
}
}
return $targets;
}
/**
* Callback for mapping. Here is where the actual mapping happens.
*
* When the callback is invoked, $target contains the name of the field the
* user has decided to map to and $value contains the value of the feed item
* element the user has picked as a source.
* Callback for mapping file fields.
*/
function file_feeds_set_target($source, $entity, $target, $value) {
if (empty($value)) {
return;
}
module_load_include('inc', 'file');
function file_feeds_set_target(FeedsSource $source, $entity, $target, array $values, array $mapping) {
$language = $mapping['language'];
// Make sure $value is an array of objects of type FeedsEnclosure.
if (!is_array($value)) {
$value = array($value);
}
foreach ($value as $k => $v) {
if (!($v instanceof FeedsEnclosure)) {
if (is_string($v)) {
$value[$k] = new FeedsEnclosure($v, file_get_mimetype($v));
}
else {
unset($value[$k]);
// Add default of uri for backwards compatibility.
list($field_name, $sub_field) = explode(':', $target . ':uri');
$info = field_info_field($field_name);
if ($sub_field == 'uri') {
foreach ($values as $k => $v) {
if (!($v instanceof FeedsEnclosure)) {
if (is_string($v)) {
$values[$k] = new FeedsEnclosure($v, file_get_mimetype($v));
}
else {
// Set the value for FALSE rather than remove it to keep our deltas
// correct.
$values[$k] = FALSE;
}
}
}
}
if (empty($value)) {
return;
}
// Determine file destination.
// @todo This needs review and debugging.
list($entity_id, $vid, $bundle_name) = entity_extract_ids($entity->feeds_item->entity_type, $entity);
$instance_info = field_info_instance($entity->feeds_item->entity_type, $target, $bundle_name);
$info = field_info_field($target);
$data = array();
if (!empty($entity->uid)) {
$data[$entity->feeds_item->entity_type] = $entity;
if ($entity instanceof Entity) {
$entity_type = $entity->entityType();
$bundle = $entity->bundle();
}
else {
$entity_type = $source->importer->processor->entityType();
$bundle = $source->importer->processor->bundle();
}
$instance_info = field_info_instance($entity_type, $field_name, $bundle);
// Determine file destination.
// @todo This needs review and debugging.
$data = array();
if (!empty($entity->uid)) {
$data[$entity_type] = $entity;
}
$destination = file_field_widget_uri($info, $instance_info, $data);
}
$destination = file_field_widget_uri($info, $instance_info, $data);
// Populate entity.
$i = 0;
$field = isset($entity->$target) ? $entity->$target : array();
foreach ($value as $v) {
try {
$file = $v->getFile($destination);
$field = isset($entity->$field_name) ? $entity->$field_name : array($language => array());
$delta = 0;
foreach ($values as $v) {
if ($info['cardinality'] == $delta) {
break;
}
catch (Exception $e) {
watchdog_exception('Feeds', $e, nl2br(check_plain($e)));
if (!isset($field[$language][$delta])) {
$field[$language][$delta] = array();
}
if ($file) {
$field['und'][$i] = (array)$file;
$field['und'][$i]['display'] = 1; // @todo: Figure out how to properly populate this field.
if ($info['cardinality'] == 1) {
switch ($sub_field) {
case 'alt':
case 'title':
case 'description':
$field[$language][$delta][$sub_field] = $v;
break;
case 'uri':
if ($v) {
try {
$v->setAllowedExtensions($instance_info['settings']['file_extensions']);
$field[$language][$delta] += (array) $v->getFile($destination);
// @todo: Figure out how to properly populate this field.
$field[$language][$delta]['display'] = 1;
}
catch (Exception $e) {
watchdog('feeds', check_plain($e->getMessage()));
}
}
break;
}
$i++;
}
$delta++;
}
$entity->{$target} = $field;
$entity->$field_name = $field;
}

View File

@@ -6,11 +6,11 @@
*/
/**
* Implements hook_feeds_processor_targets_alter().
*
* @see FeedsProcessor::getMappingTargets()
* Implements hook_feeds_processor_targets().
*/
function link_feeds_processor_targets_alter(&$targets, $entity_type, $bundle_name) {
function link_feeds_processor_targets($entity_type, $bundle_name) {
$targets = array();
foreach (field_info_instances($entity_type, $bundle_name) as $name => $instance) {
$info = field_info_field($name);
if ($info['type'] == 'link_field') {
@@ -32,45 +32,31 @@ function link_feeds_processor_targets_alter(&$targets, $entity_type, $bundle_nam
}
}
}
return $targets;
}
/**
* Callback for mapping. Here is where the actual mapping happens.
*
* When the callback is invoked, $target contains the name of the field the
* user has decided to map to and $value contains the value of the feed item
* element the user has picked as a source.
* Callback for mapping link fields.
*/
function link_feeds_set_target($source, $entity, $target, $value) {
if (empty($value)) {
return;
}
function link_feeds_set_target(FeedsSource $source, $entity, $target, array $values, array $mapping) {
$language = $mapping['language'];
// Handle non-multiple value fields.
if (!is_array($value)) {
$value = array($value);
}
// Iterate over all values.
list($field_name, $column) = explode(':', $target);
$info = field_info_field($field_name);
$field = isset($entity->$field_name) ? $entity->$field_name : array();
$field = isset($entity->$field_name) ? $entity->$field_name : array($language => array());
$delta = 0;
foreach ($value as $v) {
if ($info['cardinality'] == $delta) {
break;
foreach ($values as $value) {
if (is_object($value) && ($value instanceof FeedsElement)) {
$value = $value->getValue();
}
if (is_object($v) && ($v instanceof FeedsElement)) {
$v = $v->getValue();
}
if (is_scalar($v)) {
$field['und'][$delta][$column] = $v;
$delta++;
if (is_scalar($value)) {
$field[$language][$delta][$column] = (string) $value;
}
$delta++;
}
$entity->$field_name = $field;
}

View File

@@ -0,0 +1,76 @@
<?php
/**
* @file
* On behalf implementation of Feeds mapping API for list.module.
*/
/**
* Implements hook_feeds_processor_targets().
*/
function list_feeds_processor_targets($entity_type, $bundle_name) {
$targets = array();
foreach (field_info_instances($entity_type, $bundle_name) as $name => $instance) {
$info = field_info_field($name);
switch ($info['type']) {
case 'list_integer':
case 'list_float':
$targets[$name] = array(
'name' => check_plain($instance['label']),
'callback' => 'number_feeds_set_target',
'description' => t('The @label field of the entity.', array('@label' => $instance['label'])),
);
break;
case 'list_boolean':
$targets[$name] = array(
'name' => check_plain($instance['label']),
'callback' => 'list_feeds_set_boolean_target',
'description' => t('The @label field of the entity.', array('@label' => $instance['label'])),
);
break;
case 'list_text':
$targets[$name] = array(
'name' => check_plain($instance['label']),
'callback' => 'text_feeds_set_target',
'description' => t('The @label field of the entity.', array('@label' => $instance['label'])),
);
break;
}
}
return $targets;
}
/**
* Callback for setting list_boolean fields.
*/
function list_feeds_set_boolean_target(FeedsSource $source, $entity, $target, array $values, array $mapping) {
$language = $mapping['language'];
$field = isset($entity->$target) ? $entity->$target : array($language => array());
foreach ($values as $value) {
if (is_object($value) && ($value instanceof FeedsElement)) {
$value = $value->getValue();
}
if (is_string($value) && strlen($value) == 0) {
// Don't convert an empty string to a boolean.
continue;
}
if (is_null($value)) {
// Don't convert a NULL value to a boolean.
continue;
}
$field[$language][] = array('value' => (int) (bool) $value);
}
$entity->$target = $field;
}

View File

@@ -0,0 +1,118 @@
<?php
/**
* @file
* On behalf implementation of Feeds mapping API for locale.module.
*/
/**
* Implements hook_feeds_processor_targets_alter().
*/
function locale_feeds_processor_targets_alter(array &$targets, $entity_type, $bundle_name) {
foreach (array_keys($targets) as $target) {
$targets[$target]['preprocess_callbacks'][] = 'locale_feeds_preprocess_callback';
$targets[$target]['summary_callbacks'][] = 'locale_feeds_summary_callback';
$targets[$target]['form_callbacks'][] = 'locale_feeds_form_callback';
}
}
/**
* Preprocess callback that sets the configured mapping language.
*/
function locale_feeds_preprocess_callback(array $target, array &$mapping) {
if (empty($mapping['field_language'])) {
return;
}
$mapping['language'] = $mapping['field_language'];
}
/**
* Summary callback.
*/
function locale_feeds_summary_callback(array $mapping, array $target, array $form, array $form_state) {
$entity_type = $form_state['build_info']['args'][0]->processor->entityType();
$translatable = _locale_feeds_target_is_translatable($entity_type, $mapping['target']);
$mapping += array('field_language' => LANGUAGE_NONE);
$language_options = array(LANGUAGE_NONE => t('Language neutral')) + locale_language_list('name');
$error = NULL;
if ($mapping['field_language'] !== LANGUAGE_NONE && !$translatable) {
// This is an invalid configuration that can come from disabling
// entity_translation.
$error = t('Field not translatable');
}
if (!isset($language_options[$mapping['field_language']])) {
// This is an invalid configuration that can be caused by disabling or
// removing the language in question.
$error = t('Language \'@lang\' not available', array('@lang' => $mapping['field_language']));
}
// Nothing to see here.
if (!$error && !$translatable) {
return;
}
if ($error) {
return t('Language: <strong>Error: @error</strong>', array('@error' => $error));
}
return t('Language: %lang', array('%lang' => $language_options[$mapping['field_language']]));
}
/**
* Form callback.
*/
function locale_feeds_form_callback(array $mapping, array $target, array $form, array $form_state) {
$form = array();
$entity_type = $form_state['build_info']['args'][0]->processor->entityType();
$translatable = _locale_feeds_target_is_translatable($entity_type, $mapping['target']);
$mapping += array('field_language' => LANGUAGE_NONE);
// This is an invalid configuration that can come from disabling
// entity_translation.
$error = $mapping['field_language'] !== LANGUAGE_NONE && !$translatable;
// Nothing to see here.
if (!$error && !$translatable) {
return $form;
}
$language_options = array(LANGUAGE_NONE => t('Language neutral'));
if (!$error) {
$language_options += locale_language_list('name');
}
$form['field_language'] = array(
'#type' => 'select',
'#title' => t('Language'),
'#options' => $language_options,
'#default_value' => $mapping['field_language'],
);
return $form;
}
/**
* Determines if a target is translatable.
*
* @param string $entity_type
* The entity type.
* @param string $target
* The target.
*
* @return bool
* Returns true if the target is translatable, false if not.
*/
function _locale_feeds_target_is_translatable($entity_type, $target) {
list($field_name) = explode(':', $target, 2);
$info = field_info_field($field_name);
return !empty($info) && field_is_translatable($entity_type, $info);
}

View File

@@ -6,15 +6,12 @@
*/
/**
* Implements hook_feeds_processor_targets_alter().
*
* @see FeedsProcessor::getMappingTargets()
* Implements hook_feeds_processor_targets().
*/
function number_feeds_processor_targets_alter(&$targets, $entity_type, $bundle_name) {
function number_feeds_processor_targets($entity_type, $bundle_name) {
$targets = array();
$numeric_types = array(
'list_integer',
'list_float',
'list_boolean',
'number_integer',
'number_decimal',
'number_float',
@@ -30,43 +27,27 @@ function number_feeds_processor_targets_alter(&$targets, $entity_type, $bundle_n
);
}
}
return $targets;
}
/**
* Callback for mapping numerics.
*
* Ensure that $value is a numeric to avoid database errors.
* Callback for mapping number fields.
*/
function number_feeds_set_target($source, $entity, $target, $value) {
// Do not perform the regular empty() check here. 0 is a valid value. That's
// really just a performance thing anyway.
if (!is_array($value)) {
$value = array($value);
}
$info = field_info_field($target);
function number_feeds_set_target(FeedsSource $source, $entity, $target, array $values, array $mapping) {
$language = $mapping['language'];
// Iterate over all values.
$field = isset($entity->$target) ? $entity->$target : array('und' => array());
$field = isset($entity->$target) ? $entity->$target : array($language => array());
// Allow for multiple mappings to the same target.
$delta = count($field['und']);
foreach ($values as $value) {
foreach ($value as $v) {
if ($info['cardinality'] == $delta) {
break;
if (is_object($value) && ($value instanceof FeedsElement)) {
$value = $value->getValue();
}
if (is_object($v) && ($v instanceof FeedsElement)) {
$v = $v->getValue();
}
if (is_numeric($v)) {
$field['und'][$delta]['value'] = $v;
$delta++;
if (is_numeric($value)) {
$field[$language][] = array('value' => $value);
}
}

View File

@@ -6,11 +6,11 @@
*/
/**
* Implements hook_feeds_processor_targets_alter().
*
* @see FeedsNodeProcessor::getMappingTargets().
* Implements hook_feeds_processor_targets().
*/
function path_feeds_processor_targets_alter(&$targets, $entity_type, $bundle_name) {
function path_feeds_processor_targets($entity_type, $bundle_name) {
$targets = array();
switch ($entity_type) {
case 'node':
case 'taxonomy_term':
@@ -19,28 +19,27 @@ function path_feeds_processor_targets_alter(&$targets, $entity_type, $bundle_nam
'name' => t('Path alias'),
'description' => t('URL path alias of the node.'),
'callback' => 'path_feeds_set_target',
'summary_callback' => 'path_feeds_summary_callback',
'form_callback' => 'path_feeds_form_callback',
'summary_callbacks' => array('path_feeds_summary_callback'),
'form_callbacks' => array('path_feeds_form_callback'),
);
break;
}
return $targets;
}
/**
* Callback for mapping. Here is where the actual mapping happens.
*
* When the callback is invoked, $target contains the name of the field the
* user has decided to map to and $value contains the value of the feed item
* element the user has picked as a source.
* Callback for mapping path aliases.
*/
function path_feeds_set_target($source, $entity, $target, $value, $mapping) {
if (empty($value)) {
$value = '';
}
// Path alias cannot be multi-valued, so use the first value.
if (is_array($value)) {
$value = $value[0];
function path_feeds_set_target(FeedsSource $source, $entity, $target, array $values, array $mapping) {
$alias = FALSE;
// Path alias cannot be multi-valued, so use the first non-empty value.
foreach ($values as $value) {
$value = ltrim(trim($value), '/');
if (strlen($value)) {
$alias = $value;
break;
}
}
$entity->path = array();
@@ -58,37 +57,17 @@ function path_feeds_set_target($source, $entity, $target, $value, $mapping) {
}
}
$entity->path['pathauto'] = FALSE;
// Allow pathauto to set the path alias if the option is set, and this value
// is empty.
if (!empty($mapping['pathauto_override']) && !$value) {
$entity->path['pathauto'] = TRUE;
}
else {
$entity->path['alias'] = ltrim($value, '/');
}
// Allow pathauto to set the path alias if the option is set, and the value is
// empty.
$entity->path['pathauto'] = !empty($mapping['pathauto_override']) && $alias === FALSE;
$entity->path['alias'] = (string) $alias;
}
/**
* Mapping configuration summary for path.module.
*
* @param $mapping
* Associative array of the mapping settings.
* @param $target
* Array of target settings, as defined by the processor or
* hook_feeds_processor_targets_alter().
* @param $form
* The whole mapping form.
* @param $form_state
* The form state of the mapping form.
*
* @return
* Returns, as a string that may contain HTML, the summary to display while
* the full form isn't visible.
* If the return value is empty, no summary and no option to view the form
* will be displayed.
*/
function path_feeds_summary_callback($mapping, $target, $form, $form_state) {
function path_feeds_summary_callback(array $mapping, $target, array $form, array $form_state) {
if (!module_exists('pathauto')) {
return;
}
@@ -104,12 +83,8 @@ function path_feeds_summary_callback($mapping, $target, $form, $form_state) {
/**
* Settings form callback.
*
* @return
* The per mapping configuration form. Once the form is saved, $mapping will
* be populated with the form values.
*/
function path_feeds_form_callback($mapping, $target, $form, $form_state) {
function path_feeds_form_callback(array $mapping, $target, array $form, array $form_state) {
return array(
'pathauto_override' => array(
'#type' => 'checkbox',

View File

@@ -2,16 +2,17 @@
/**
* @file
* On behalf implementation of Feeds mapping API for user profiles.
* On behalf implementation of Feeds mapping API for profile.module.
*/
/**
* Implements hook_feeds_processor_target_alter().
* Implements hook_feeds_processor_targets().
*/
function profile_feeds_processor_targets_alter(&$targets, $entity_type, $bundle_name) {
function profile_feeds_processor_targets($entity_type, $bundle_name) {
$targets = array();
if ($entity_type != 'user') {
return;
return $targets;
}
$categories = profile_user_categories();
@@ -25,11 +26,13 @@ function profile_feeds_processor_targets_alter(&$targets, $entity_type, $bundle_
);
}
}
return $targets;
}
/**
* Set the user profile target after import.
*/
function profile_feeds_set_target($source, $entity, $target, $value, $mapping) {
$entity->$target = $value;
function profile_feeds_set_target(FeedsSource $source, $entity, $target, array $values) {
$entity->$target = reset($values);
}

View File

@@ -2,15 +2,28 @@
/**
* @file
* Mapper that exposes a node's taxonomy vocabularies as mapping targets.
* On behalf implementation of Feeds mapping API for taxonomy.module.
*/
/**
* Implements hook_feeds_parser_sources_alter().
*
* @todo: Upgrade to 7.
* Search by term name.
*/
function taxonomy_feeds_parser_sources_alter(&$sources, $content_type) {
define('FEEDS_TAXONOMY_SEARCH_TERM_NAME', 0);
/**
* Search by term id.
*/
define('FEEDS_TAXONOMY_SEARCH_TERM_ID', 1);
/**
* Search by GUID.
*/
define('FEEDS_TAXONOMY_SEARCH_TERM_GUID', 2);
/**
* Implements hook_feeds_parser_sources_alter().
*/
function taxonomy_feeds_parser_sources_alter(array &$sources, $content_type) {
if (!empty($content_type)) {
foreach (taxonomy_get_vocabularies($content_type) as $vocabulary) {
$sources['parent:taxonomy:' . $vocabulary->machine_name] = array(
@@ -36,77 +49,161 @@ function taxonomy_feeds_get_source(FeedsSource $source, FeedsParserResult $resul
$result[] = new FeedsTermElement($term);
}
}
return $result;
}
}
/**
* Implements hook_feeds_processor_targets_alter().
* Implements hook_feeds_processor_targets().
*/
function taxonomy_feeds_processor_targets_alter(&$targets, $entity_type, $bundle_name) {
function taxonomy_feeds_processor_targets($entity_type, $bundle_name) {
$targets = array();
foreach (field_info_instances($entity_type, $bundle_name) as $name => $instance) {
$info = field_info_field($name);
if ($info['type'] == 'taxonomy_term_reference') {
$targets[$name] = array(
'name' => check_plain($instance['label']),
'callback' => 'taxonomy_feeds_set_target',
'description' => t('The @label field of the node.', array('@label' => $instance['label'])),
'description' => t('The @label field of the entity.', array('@label' => $instance['label'])),
'summary_callbacks' => array('taxonomy_feeds_summary_callback'),
'form_callbacks' => array('taxonomy_feeds_form_callback'),
);
}
}
if ($entity_type == 'taxonomy_term') {
$targets['tid']['name'] = t('Term id');
$targets['tid']['description'] = t('The tid of the taxonomy term. NOTE: use this feature with care, node ids are usually assigned by Drupal.');
unset($targets['vocabulary']);
}
return $targets;
}
/**
* Callback for mapping. Here is where the actual mapping happens.
*
* @todo Do not create new terms for non-autotag fields.
* Callback for mapping taxonomy terms.
*/
function taxonomy_feeds_set_target($source, $entity, $target, $terms) {
if (empty($terms)) {
return;
}
function taxonomy_feeds_set_target(FeedsSource $source, $entity, $target, array $terms, array $mapping) {
$language = $mapping['language'];
// Handle non-multiple values.
if (!is_array($terms)) {
$terms = array($terms);
}
// Add in default values.
$mapping += array(
'term_search' => FEEDS_TAXONOMY_SEARCH_TERM_NAME,
'autocreate' => FALSE,
);
$info = field_info_field($target);
// See http://drupal.org/node/881530
if (isset($info['settings']['allowed_values'][0]['vocabulary'])) {
$vocabulary = taxonomy_vocabulary_machine_name_load($info['settings']['allowed_values'][0]['vocabulary']);
}
else {
$vocabulary = taxonomy_vocabulary_load($info['settings']['allowed_values'][0]['vid']);
$cache = &drupal_static(__FUNCTION__);
if (!isset($cache['allowed_values'][$target])) {
$cache['allowed_values'][$target] = taxonomy_allowed_values($info);
}
$i = 0;
$entity->$target = isset($entity->$target) ? $entity->$target : array();
if (!isset($cache['allowed_vocabularies'][$target])) {
foreach ($info['settings']['allowed_values'] as $tree) {
if ($vocabulary = taxonomy_vocabulary_machine_name_load($tree['vocabulary'])) {
$cache['allowed_vocabularies'][$target][$vocabulary->vid] = $vocabulary->machine_name;
}
}
}
// Some kind of configuration issue. Perhaps the vocabulary was deleted.
// Nothing we can do about it.
if (empty($cache['allowed_vocabularies'][$target])) {
return;
}
$query = new EntityFieldQuery();
$query->entityCondition('entity_type', 'taxonomy_term')
->entityCondition('bundle', $cache['allowed_vocabularies'][$target])
->range(0, 1);
$field = isset($entity->$target) ? $entity->$target : array($language => array());
if (!isset($field[$language])) {
$field[$language] = array();
}
// Allow for multiple mappings to the same target.
$delta = count($field[$language]);
// Iterate over all values.
foreach ($terms as $term) {
$tid = 0;
if ($info['cardinality'] == $delta) {
break;
}
$tid = FALSE;
// FeedsTermElement already is a term.
if ($term instanceof FeedsTermElement) {
$tid = $term->tid;
}
elseif (is_numeric($term)) {
$tid = $term;
}
elseif (is_string($term)) {
$tid = taxonomy_term_check_term($term, $vocabulary->vid);
}
if ($tid) {
$entity->{$target}['und'][$i]['tid'] = $tid;
else {
switch ($mapping['term_search']) {
// Lookup by name.
case FEEDS_TAXONOMY_SEARCH_TERM_NAME:
$term = trim($term);
$name_query = clone $query;
if (strlen($term) && $tids = $name_query->propertyCondition('name', $term)->execute()) {
// Find the first allowed term.
foreach ($tids['taxonomy_term'] as $possible_term) {
if (isset($cache['allowed_values'][$target][$possible_term->tid])) {
$tid = $possible_term->tid;
break;
}
}
}
elseif ($mapping['autocreate'] && strlen($term)) {
$term = (object) array(
'name' => drupal_substr($term, 0, 255),
'vid' => key($cache['allowed_vocabularies'][$target]),
'vocabulary_machine_name' => reset($cache['allowed_vocabularies'][$target]),
);
// Set language if the taxonomy is multilingual.
if ($language !== LANGUAGE_NONE) {
$info = entity_get_info('taxonomy_term');
if (!empty($info['entity keys']['language'])) {
$term->{$info['entity keys']['language']} = $language;
}
}
taxonomy_term_save($term);
$tid = $term->tid;
// Add to the list of allowed values.
$cache['allowed_values'][$target][$tid] = $term->name;
}
break;
// Lookup by tid.
case FEEDS_TAXONOMY_SEARCH_TERM_ID:
if (is_numeric($term)) {
$tid = (int) $term;
}
break;
// Lookup by GUID.
case FEEDS_TAXONOMY_SEARCH_TERM_GUID:
$tid = taxonomy_feeds_term_lookup_term_by_guid($term);
break;
}
}
if ($info['cardinality'] == 1) {
break;
if ($tid && isset($cache['allowed_values'][$target][$tid])) {
$field[$language][] = array('tid' => $tid);
$delta++;
}
$i++;
}
$entity->$target = $field;
}
/**
* Find all terms associated with the given node, within one vocabulary.
* Finds all terms associated with the given node, within one vocabulary.
*/
function taxonomy_feeds_node_get_terms($node, $key = 'tid') {
$terms = &drupal_static(__FUNCTION__);
@@ -118,7 +215,7 @@ function taxonomy_feeds_node_get_terms($node, $key = 'tid') {
foreach ($fields as $field_name => $field) {
if ($field['type'] == 'taxonomy_term_reference' && field_info_instance('node', $field_name, $node->type)) {
if (($items = field_get_items('node', $node, $field_name)) && is_array($items)) {
$tids = array_merge($tids, array_map('_taxonomy_extract_tid', $items));
$tids = array_merge($tids, array_map('_taxonomy_feeds_extract_tid', $items));
}
}
}
@@ -134,53 +231,86 @@ function taxonomy_feeds_node_get_terms($node, $key = 'tid') {
}
/**
* Helper function used in taxonomy_feeds_node_get_terms(). Extracts
* tid from array item returned by field_get_items().
* Extracts tid from array item returned by field_get_items().
*
* @param $item tid information in a form of single element array (key == 'tid', value == tid we're looking for)
* @param array $item
* Tid information in the form of a single element array
* (key == 'tid', value == tid we're looking for)
*
* @return tid extracted from $item.
* @return int
* Term id extracted from $item.
*
* @see taxonomy_feeds_node_get_terms()
* @see field_get_items()
*/
function _taxonomy_extract_tid($item) {
function _taxonomy_feeds_extract_tid($item) {
return $item['tid'];
}
/**
* Checks whether a term identified by name and vocabulary exists. Creates a
* new term if it does not exist.
* Looks up a term by GUID, assumes SQL storage backend.
*
* @param $name
* A term name.
* @param $vid
* A vocabulary id.
* @param string $guid
* The Feeds GUID to compare against.
*
* @return
* A term id.
* @return int|FALSE
* The term id, or FALSE if one was not found.
*/
function taxonomy_term_check_term($name, $vid) {
$name = trim($name);
$term = taxonomy_term_lookup_term($name, $vid);
if (empty($term)) {
$term = new stdClass();
$term->name = $name;
$term->vid = $vid;
taxonomy_term_save($term);
return $term->tid;
}
return $term->tid;
function taxonomy_feeds_term_lookup_term_by_guid($guid) {
return db_select('feeds_item')
->fields('feeds_item', array('entity_id'))
->condition('entity_type', 'taxonomy_term')
->condition('guid', $guid)
->execute()
->fetchField();
}
/**
* Looks up a term, assumes SQL storage backend.
* Mapping configuration summary for taxonomy.module.
*/
function taxonomy_term_lookup_term($name, $vid) {
return db_select('taxonomy_term_data', 'td')
->fields('td', array('tid', 'name'))
->condition('name', $name)
->condition('vid', $vid)
->execute()
->fetchObject();
}
function taxonomy_feeds_summary_callback(array $mapping, $target, array $form, array $form_state) {
$options = _taxonomy_feeds_form_callback_options();
if (empty($mapping['term_search'])) {
return t('Search taxonomy terms by: <strong>@search</strong>', array('@search' => $options[FEEDS_TAXONOMY_SEARCH_TERM_NAME]));
}
return t('Search taxonomy terms by: <strong>@search</strong>', array('@search' => $options[$mapping['term_search']]));
}
/**
* Settings form callback.
*/
function taxonomy_feeds_form_callback(array $mapping, $target, array $form, array $form_state) {
return array(
'term_search' => array(
'#type' => 'select',
'#title' => t('Search taxonomy terms by'),
'#options' => _taxonomy_feeds_form_callback_options(),
'#default_value' => !empty($mapping['term_search']) ? $mapping['term_search'] : FEEDS_TAXONOMY_SEARCH_TERM_NAME,
),
'autocreate' => array(
'#type' => 'checkbox',
'#title' => t('Auto create'),
'#description' => t("Create the term if it doesn't exist."),
'#default_value' => !empty($mapping['autocreate']) ? $mapping['autocreate'] : 0,
'#states' => array(
'visible' => array(
':input[name$="[settings][term_search]"]' => array('value' => FEEDS_TAXONOMY_SEARCH_TERM_NAME),
),
),
),
);
}
/**
* Returns the list of available term search methods.
*
* @return array
* An array of taxonomy search option titles.
*/
function _taxonomy_feeds_form_callback_options() {
return array(
FEEDS_TAXONOMY_SEARCH_TERM_NAME => 'Term name',
FEEDS_TAXONOMY_SEARCH_TERM_ID => 'Term ID',
FEEDS_TAXONOMY_SEARCH_TERM_GUID => 'GUID',
);
}

View File

@@ -6,13 +6,12 @@
*/
/**
* Implements hook_feeds_processor_targets_alter().
*
* @see FeedsProcessor::getMappingTargets()
* Implements hook_feeds_processor_targets().
*/
function text_feeds_processor_targets_alter(&$targets, $entity_type, $bundle_name) {
function text_feeds_processor_targets($entity_type, $bundle_name) {
$targets = array();
$text_types = array(
'list_text',
'text',
'text_long',
'text_with_summary',
@@ -26,54 +25,118 @@ function text_feeds_processor_targets_alter(&$targets, $entity_type, $bundle_nam
'callback' => 'text_feeds_set_target',
'description' => t('The @label field of the entity.', array('@label' => $instance['label'])),
);
if ($info['type'] == 'text_with_summary') {
// Allow mapping to summary.
$targets[$name . ':summary'] = array(
'name' => t('@name: Summary', array('@name' => $instance['label'])),
'callback' => 'text_feeds_set_target',
'description' => t('The @label field of the entity.', array('@label' => $instance['label'])),
'real_target' => $name,
);
}
}
if (!empty($instance['settings']['text_processing'])) {
$targets[$name]['summary_callbacks'] = array('text_feeds_summary_callback');
$targets[$name]['form_callbacks'] = array('text_feeds_form_callback');
}
}
return $targets;
}
/**
* Callback for mapping text fields.
*/
function text_feeds_set_target($source, $entity, $target, $value) {
if (empty($value)) {
return;
}
function text_feeds_set_target(FeedsSource $source, $entity, $target, array $values, array $mapping) {
$language = $mapping['language'];
if (!is_array($value)) {
$value = array($value);
}
list($field_name, $column) = explode(':', $target . ':value');
if (isset($source->importer->processor->config['input_format'])) {
if ($column === 'value' && isset($source->importer->processor->config['input_format'])) {
$format = $source->importer->processor->config['input_format'];
// Add in default values.
$mapping += array(
'format' => $format,
);
}
$info = field_info_field($target);
$field = isset($entity->$field_name) ? $entity->$field_name : array($language => array());
// Iterate over all values.
$field = isset($entity->$target) ? $entity->$target : array('und' => array());
$delta = 0;
foreach ($values as $value) {
// Allow for multiple mappings to the same target.
$delta = count($field['und']);
foreach ($value as $v) {
if ($info['cardinality'] == $delta) {
break;
if (is_object($value) && $value instanceof FeedsElement) {
$value = $value->getValue();
}
if (is_object($v) && ($v instanceof FeedsElement)) {
$v = $v->getValue();
}
if (is_scalar($value) && strlen($value)) {
if (is_scalar($v)) {
$field['und'][$delta]['value'] = $v;
$field[$language][$delta][$column] = (string) $value;
if (isset($format)) {
$field['und'][$delta]['format'] = $format;
if (isset($mapping['format'])) {
$field[$language][$delta]['format'] = $mapping['format'];
}
$delta++;
}
$delta++;
}
$entity->$target = $field;
$entity->$field_name = $field;
}
/**
* Summary callback for text field targets.
*
* Displays which text format will be used for the text field target.
*
* @see text_feeds_processor_targets()
* @see text_feeds_form_callback()
*/
function text_feeds_summary_callback(array $mapping, $target, array $form, array $form_state) {
global $user;
$formats = filter_formats($user);
// Processor-wide input format setting.
$importer = feeds_importer($form['#importer']);
$default_format = !empty($importer->processor->config['input_format']) ? $importer->processor->config['input_format'] : filter_fallback_format();
$mapping += array(
'format' => $default_format,
);
return t('Text format: %format', array('%format' => $formats[$mapping['format']]->name));
}
/**
* Form callback for text field targets.
*
* Allows to select a text format for the text field target.
*
* @see text_feeds_processor_targets()
* @see text_feeds_summary_callback()
*/
function text_feeds_form_callback(array $mapping, $target, array $form, array $form_state) {
global $user;
$formats_options = array();
$formats = filter_formats($user);
foreach ($formats as $id => $format) {
$formats_options[$id] = $format->name;
}
// Processor-wide text format setting.
$importer = feeds_importer($form['#importer']);
$default_format = !empty($importer->processor->config['input_format']) ? $importer->processor->config['input_format'] : filter_fallback_format();
$mapping += array(
'format' => $default_format,
);
return array(
'format' => array(
'#type' => 'select',
'#title' => t('Text format'),
'#options' => $formats_options,
'#default_value' => $mapping['format'],
),
);
}