Problem/Motivation

Taxonomy term reference field are not currently supported for mapping (see #2563065: Not all field types are supported by mapping).

This could be useful for categorising content, for example linking imported nodes to users.

Proposed resolution

Provide support for taxonomy term fields by looking up their term id.

Remaining tasks

  • Detect if field being mapped to is a taxonomy term field
  • Lookup term id from text value, and use this as value.

User interface changes

None

API changes

None

Data model changes

None

Support from Acquia helps fund testing for Drupal Acquia logo

Comments

ydahi created an issue. See original summary.

Leon Kessler’s picture

Not fully understanding what you're trying to do here. Why does your user field need to be a taxonomy term field? Seems like it would be fine to be entity reference field.

ydahi’s picture

Thanks for your question. Yes, I can swap the field to be a entity reference instead of a taxonomy term reference.

I'll try and elaborate on my use case:

  1. I'm trying to create a views block that shows tweet nodes filtered by tweet_user.
  2. This views block will show on content type "profile" which has the field referenced_tweet_ user.
  3. Using this field, I want contextually pass the selected tweet_user ID, Name, TID, whatever to the views block.
  4. Such that, when creating a profile node and the user selects the values "drupal" and "wordpress" for the referenced_tweet_user field, the block view will show all tweet nodes for those selected values values.

So far my approach has been as follows:

  1. Create vocabulary called twitter accounts
  2. Add twitter account name as term to this vocabulary
  3. Create a new field on tweet content type called tweet_accountname which is an entity reference for bundle term
  4. Create a rule to autotag the tweet node based on field_tweet_user + twitter account vocabulary (use module Rules Autotag)
  5. Create content type called Profile with entity reference field field_referenced_twitter of bundle term
  6. Create a view to show tweet nodes that have the same tag (or tweet_user) as in the field_referenced_twitter

That last step is where I'm having problems. I'm also hoping that there is a better way to do this. Any help/guidance would be greatly appreciated.

Leon Kessler’s picture

Is the issue here that you're not able to map to a taxonomy term reference field? (And that's why you're looking at resorting to options like Rules autotag)?

If that is the case then we can turn this into a feature request and update the summary.

ydahi’s picture

Title: Save tweet_user as Taxonomy Term » Map field_tweet_user to Taxonomy Term Reference Field
Category: Support request » Feature request

Sure thing, that sounds more accurate.

Updating issue to reflect real purpose. The creation of the view is out of the scope of this issue any way.

ydahi’s picture

Issue summary: View changes
Leon Kessler’s picture

Title: Map field_tweet_user to Taxonomy Term Reference Field » Allow mapping to Taxonomy Term Reference Field
Version: 7.x-2.0-beta6 » 7.x-2.x-dev
Issue summary: View changes
Related issues: +#2563065: Not all field types are supported by mapping

Have updated issue as this should apply to all fields, not just tweet_user.

Also adding related issue that deals with not all field types being supported.

bgrobertson’s picture

@ydahi, i was trying to do something similar. Now i'm trying to switch to an entity reference field instead, but I can't seem to get it to work. Can you post your code for how you are creating/saving those fields?

ydahi’s picture

@bgrobertson, I don't think any code has been written yet - this is still a feature request.

I ended up using the following tools to build the view:
- TT RSS Twitter feeds parser (https://github.com/jdelamater99/Twitter-RSS-Parser)
- Feeds
- Rules Autotag
- Views field Views

I use the the Feeds module to parse the RSS feed built with TTRSS parser. So far this has been working great - even the workflow for adding more hashtags and twitter accounts is easy enough for average users to perform. Hope that helps!

bgrobertson’s picture

I was able to get this to work with entity reference fields, problem is it only seems to work sometimes. I'm using the entity reference module and the entity reference autocomplete module.

Here's my code:

function upup_taxonomy_social_content_settings_alter(&$type, &$form, &$settings) {
  switch ($type) {
    case 'instance':
      $form['field_up_up_client'] = array(
        '#type' => 'entityreference',
        '#title' => t('Up&Up Client'),
        '#era_entity_type' => 'node',  // Mandatory.
        '#era_bundles' => array('up_up_client'), // Optional (Any bundle by default).
        '#era_cardinality' => 1,       // Optional (1 By default).
        '#description' => 'Choose a client name.',
        '#default_value' => isset($settings['field_up_up_client']) ? $settings['field_up_up_client'] : NULL,
      );

       $form['field_up_up_contact'] = array(
        '#type' => 'entityreference', 
        '#title' => t('Up&Up Contact'), 
        '#era_entity_type' => 'node',  // Mandatory.
        '#era_bundles' => array('up_up_contact'), // Optional (Any bundle by default).
        '#era_cardinality' => 1,       // Optional (1 By default).
        '#description' => 'Enter a contact name. This is optional.',
        '#default_value' => isset($settings['field_up_up_contact']) ? $settings['field_up_up_contact'] : NULL,
      );
      break;
  }
}

/**
 * Alter the entity before it's saved.
 *
 * You have the original record so you can grab fields from it and attach it to
 *  the wrapper.
 *
 * @param EntityMetadataWrapper $wrapper
 *   The EntityMetadataWrapper with the fields in the settings set.
 * @param object $context
 *   The original record from the social network.
 */
function upup_taxonomy_social_content_import_alter(&$wrapper, &$context) {
  $settings = $context['settings'];
  if (isset($settings['field_up_up_client']) && !empty($settings['field_up_up_client'])) {
    $wrapper->field_up_up_client->set(array_values($settings['field_up_up_client'])[0]);
  }
  if (isset($settings['field_up_up_contact']) && !empty($settings['field_up_up_contact'])) {
    $wrapper->field_up_up_contact->set(array_values($settings['field_up_up_contact'])[0]);
  }
}
iampuma’s picture

I created the following patch for mapping hashtags to taxonomy field terms. It works as the following proposal above and in addition creates the term if it does not exist.
This patch has only been tested with Instagram.

  • Pere Orga committed 7d802b4 on 7.x-2.x authored by iampuma
    Issue #2561977 by iampuma, ydahi, Leon Kessler, Pere Orga: Allow mapping...
Pere Orga’s picture

Status: Active » Fixed

Committed #11 with the following:

@@ -687,6 +687,9 @@
         elseif (in_array($property_info[$field_name]['type'], array('list<taxonomy_term>', 'taxonomy_term'))) {
           $terms = array();
           $vid = taxonomy_vocabulary_machine_name_load($property_info[$field_name]['bundle'])->vid;
+          if ($value && !is_array($value)) {
+            $value = array($value);
+          }
           foreach ($value as $name) {
             $name = self::validateText($name);
             $term = taxonomy_get_term_by_name($name, $property_info[$field_name]['bundle']);

Otherwise it fails when $value is a single item.

Thanks.

Status: Fixed » Closed (fixed)

Automatically closed - issue fixed for 2 weeks with no activity.