2

Hi i have 2 content types:

  • Agent
  • Organisation

they both have fields for id:

  • ID ( agent content type)
  • Org ID ( Organisation content type)

Now i need to create a view that will list fields from both of the content types, and only relation between them is same value in fields ( field_id and field_org_id ).

Since content is already there and there are more than 200 nodes in each content type, i need to work with what i have. That means i cant add new fields ( like node reference ).

Is there a way to make a view that will list fields from two content types that share same value in 2 fields with different name? And in each row to have mix of fields from both content type with relation of this ID.

  • try adding both of those fields ID and Org ID in FILTERS. – No Sssweat Oct 4 '16 at 6:07
1

Since field_id and field_org_id are two separate fields, you'll need some way to tie them together. A custom entity could do that, but you could also cheat a little and use a taxonomy vocabulary that contains these two fields.

You could then build a view that displays all content and add a relation to the vocabulary and restrict the results to only the content that possesses this relationship.

Cheaty, but I think it might be the easiest solution.

Edit:

I wouldn't modify the content or content types, but rather, add the field_id and field_org_id fields to the taxonomy vocabulary, then iterate through your Agent and Organization nodes and generate taxonomy terms that associate one field's value to another by populating each term's field_id and field_org_id fields from their respective Agent and Organization nodes.

So, some code...

Create the vocabulary

$vocab = new stdClass();
$vocab->name = 'Organization to Agents';
$vocab->machine_name = 'org_to_agents';
taxonomy_vocabulary_save($vocab);

Add the fields

$fields = array(
    'field_id',
    'field_org_id',
);

foreach($fields as $field) {
  // Replace my)taxonomy_vocabulary with your chosen vocabulary name.
  if (is_null(field_info_instance('taxonomy_term', $field, 'my_taxonomy_vocabulary'))) {
    field_create_instance(array(
      'label' => $t($field),  // <-- This doesn't need to be pretty.
      'widget' => array(
        'type' => 'textfield',
      ),
      'field_name' => $field,
      'entity_type' => 'taxonomy_term',
      'bundle' => 'my_taxonomy_vocabulary',
    ));
  }
}

Get the list of IDs

// Credits to https://www.drupal.org/node/1079616#comment-10864932 for example.
$agents = db_query("SELECT nid FROM {node} WHERE type = :type", array(':type' => 'agent'))
  ->fetchCol();
$orgs = db_query("SELECT nid FROM {node} WHERE type = :type", array(':type' => 'organization'))
  ->fetchCol();

// Should result in a list of unique Node IDs that exist in
// at least one node for both content types.
$nids = array_intersect($agents, $orgs);

Create your taxonomy terms

$vocab = taxonomy_vocabulary_machine_name_load('my_taxonomy_vocabulary');
foreach($nids as $key => $nid) {
  $term = (object)array(
    'name' => $key, // Use any unique value that makes sense here.
    'vid' => $vocab->vid,
  );
  $term->field_id[LANGUAGE_NONE][0]['value'] = $nid;
  $term->field_org_id[LANGUAGE_NONE][0]['value'] = $nid;
  taxonomy_term_save($term);
}

Again, this is a bit "cheaty", and a custom entity would probably be considered more proper, but I think this should help.

Hope it helps!

  • Ok taxonomy vocabulary sounds doable, but can you explain a bit more how to implement it. Should i add new field in both of the content types and than somehow map it to this existing or something else? – Alex Sep 30 '16 at 11:24
  • Alex, I've updated my answer with some code and additional explanation. – Beau Oct 2 '16 at 1:07

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.