Projet

Général

Profil

Paste
Télécharger (2,69 ko) Statistiques
| Branche: | Révision:

root / drupal7 / sites / all / modules / ctools / plugins / relationships / terms_from_node.inc @ 560c3060

1
<?php
2

    
3
/**
4
 * @file
5
 * Plugin to provide an relationship handler for all terms from node.
6
 */
7

    
8
/**
9
 * Plugins are described by creating a $plugin array which will be used
10
 * by the system that includes this file.
11
 */
12
$plugin = array(
13
  'title' => t('Multiple terms from node'),
14
  'keyword' => 'terms',
15
  'description' => t('Adds a taxonomy terms from a node context; if multiple terms are selected, they wil be concatenated.'),
16
  'required context' => new ctools_context_required(t('Node'), 'node'),
17
  'context' => 'ctools_terms_from_node_context',
18
  'edit form' => 'ctools_terms_from_node_settings_form',
19
  'defaults' => array('vocabulary' => array(), 'concatenator' => ','),
20
);
21

    
22
/**
23
 * Return a new context based on an existing context.
24
 */
25
function ctools_terms_from_node_context($context, $conf) {
26
  // If unset it wants a generic, unfilled context, which is just NULL.
27
  if (empty($context->data)) {
28
    return ctools_context_create_empty('terms', NULL);
29
  }
30

    
31
  // Collect all terms for the chosen vocabulary and concatenate them.
32
  $node = $context->data;
33
  $terms = array();
34
  
35
  $fields = field_info_instances('node', $node->type);
36
  foreach ($fields as $name => $info) {
37
    $field_info = field_info_field($name);
38
    if ($field_info['type'] == 'taxonomy_term_reference' && (empty($conf['vocabulary']) || !empty($conf['vocabulary'][$field_info['settings']['allowed_values'][0]['vocabulary']]))) {
39
      $items = field_get_items('node', $node, $name);
40
      if (is_array($items)) {
41
        foreach ($items as $item) {
42
          $terms[] = $item['tid'];
43
        }
44
      }
45
    }
46
  }
47
  
48
  if (!empty($terms)) {
49
    $all_terms = ctools_break_phrase(implode($conf['concatenator'], $terms));
50
    return ctools_context_create('terms', $all_terms);
51
  }
52
}
53

    
54
/**
55
 * Settings form for the relationship.
56
 */
57
function ctools_terms_from_node_settings_form($form, &$form_state) {
58
  $conf = $form_state['conf'];
59

    
60
  $options = array();
61
  foreach (taxonomy_vocabulary_get_names() as $name => $vocabulary) {
62
    $options[$name] = $vocabulary->name;
63
  }
64
  $form['vocabulary'] = array(
65
    '#title' => t('Vocabulary'),
66
    '#type' => 'checkboxes',
67
    '#options' => $options,
68
    '#default_value' => $conf['vocabulary'],
69
    '#prefix' => '<div class="clearfix">',
70
    '#suffix' => '</div>',
71
  );
72
  $form['concatenator'] = array(
73
    '#title' => t('Concatenator'),
74
    '#type' => 'select',
75
    '#options' => array(',' => ', (AND)', '+' => '+ (OR)'),
76
    '#default_value' => $conf['concatenator'],
77
    '#prefix' => '<div class="clearfix">',
78
    '#suffix' => '</div>',
79
    '#description' => t("When the value from this context is passed on to a view as argument, the terms can be concatenated in the form of 1+2+3 (for OR) or 1,2,3 (for AND)."),
80
  );
81

    
82
  return $form;
83
}