View source
<?php
use Drupal\feeds\FeedsSource;
use Drupal\feeds\FeedsParserResult;
use Drupal\feeds\FeedsTermElement;
use Drupal\taxonomy\Type\TaxonomyTermReferenceItem;
define('FEEDS_TAXONOMY_SEARCH_TERM_NAME', 0);
define('FEEDS_TAXONOMY_SEARCH_TERM_ID', 1);
define('FEEDS_TAXONOMY_SEARCH_TERM_GUID', 2);
function taxonomy_feeds_parser_sources_alter(&$sources, $content_type) {
if (!empty($content_type)) {
foreach (field_info_instances('node', $content_type) as $name => $instance) {
$info = field_info_field($name);
if ($info['type'] == 'taxonomy_term_reference') {
$sources['parent:taxonomy:' . $info
->label()] = array(
'name' => t('Feed node: Taxonomy: @vocabulary', array(
'@vocabulary' => $instance
->label(),
)),
'description' => t('Taxonomy terms from feed node.'),
'callback' => 'taxonomy_feeds_get_source',
);
}
}
}
}
function taxonomy_feeds_get_source(FeedsSource $source, FeedsParserResult $result, $key) {
if ($node = node_load($source->feed_nid)
->getNGEntity()) {
list(, , $field) = explode(':', $key, 3);
$result = array();
foreach ($node->{$field} as $term) {
$result[] = $term;
}
return $result;
}
}
function taxonomy_feeds_processor_targets_alter(&$targets, $entity_type, $bundle_name) {
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 entity.', array(
'@label' => $instance['label'],
)),
'summary_callback' => 'taxonomy_feeds_summary_callback',
'form_callback' => 'taxonomy_feeds_form_callback',
);
}
}
}
function taxonomy_feeds_set_target($source, $entity, $target, $terms, $mapping = array()) {
if (empty($terms) && $terms != 0) {
return;
}
if (!is_array($terms)) {
$terms = array(
$terms,
);
}
$mapping += array(
'term_search' => FEEDS_TAXONOMY_SEARCH_TERM_NAME,
'autocreate' => FALSE,
);
$info = field_info_field($target);
$cache =& drupal_static(__FUNCTION__);
if (!isset($cache['allowed_values'][$target])) {
$instance = field_info_instance($entity
->entityType(), $target, $entity
->bundle());
$cache['allowed_values'][$target] = taxonomy_allowed_values($info, $instance, $entity);
}
if (!isset($cache['allowed_vocabularies'][$target])) {
foreach ($info['settings']['allowed_values'] as $tree) {
if ($vocabulary = entity_load('taxonomy_vocabulary', $tree['vocabulary'])) {
$cache['allowed_vocabularies'][$target][$vocabulary
->id()] = $vocabulary
->id();
}
}
}
$query = Drupal::entityQuery('taxonomy_term');
$query
->condition('vid', $cache['allowed_vocabularies'][$target])
->range(0, 1);
$field = isset($entity->{$target}) ? $entity->{$target} : array(
'und' => array(),
);
$delta = count($field['und']);
foreach ($terms as $term) {
if ($info['cardinality'] == $delta) {
break;
}
$tid = FALSE;
if ($term instanceof FeedsTermElement || $term instanceof TaxonomyTermReferenceItem) {
$tid = $term->tid;
}
else {
switch ($mapping['term_search']) {
case FEEDS_TAXONOMY_SEARCH_TERM_NAME:
$name_query = clone $query;
if ($tids = $name_query
->condition('name', $term)
->execute()) {
$tid = key($tids);
}
elseif ($mapping['autocreate']) {
$term = entity_create('taxonomy_term', array(
'name' => $term,
'vid' => key($cache['allowed_vocabularies'][$target]),
));
$term
->save();
$tid = $term
->id();
$cache['allowed_values'][$target][$tid] = $term
->label();
}
break;
case FEEDS_TAXONOMY_SEARCH_TERM_ID:
if (is_numeric($term)) {
$tid = $term;
}
break;
case FEEDS_TAXONOMY_SEARCH_TERM_GUID:
$tid = taxonomy_feeds_term_lookup_term_by_guid($term);
break;
}
}
if ($tid && isset($cache['allowed_values'][$target][$tid])) {
$field['und'][$delta]['tid'] = $tid;
$delta++;
}
}
$entity->{$target} = $field;
}
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();
}
function taxonomy_feeds_summary_callback($mapping, $target, $form, $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']],
));
}
function taxonomy_feeds_form_callback($mapping, $target, $form, $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,
),
),
),
),
);
}
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',
);
}