function ctools_terms_from_node_context

Return a new context based on an existing context.

1 string reference to 'ctools_terms_from_node_context'
terms_from_node.inc in plugins/relationships/terms_from_node.inc
Plugin to provide an relationship handler for all terms from node.

File

plugins/relationships/terms_from_node.inc, line 25

Code

function ctools_terms_from_node_context($context, $conf) {
    // If unset it wants a generic, unfilled context, which is just NULL.
    if (empty($context->data)) {
        return ctools_context_create_empty('terms', NULL);
    }
    // Collect all terms for the chosen vocabulary and concatenate them.
    $node = $context->data;
    $terms = array();
    $fields = field_info_instances('node', $node->type);
    foreach ($fields as $name => $info) {
        $field_info = field_info_field($name);
        if ($field_info['type'] == 'taxonomy_term_reference' && (empty($conf['vocabulary']) || !empty($conf['vocabulary'][$field_info['settings']['allowed_values'][0]['vocabulary']]))) {
            $items = field_get_items('node', $node, $name);
            if (is_array($items)) {
                foreach ($items as $item) {
                    $terms[] = $item['tid'];
                }
            }
        }
        elseif ($field_info['type'] == 'entityreference' && $field_info['settings']['target_type'] == 'taxonomy_term') {
            $items = field_get_items('node', $node, $name);
            if (is_array($items)) {
                $tids = array();
                foreach ($items as $item) {
                    $tids[] = $item['target_id'];
                }
                $term_objects = taxonomy_term_load_multiple($tids);
                foreach ($term_objects as $term) {
                    if (empty($conf['vocabulary']) || in_array($term->vocabulary_machine_name, $conf['vocabulary'])) {
                        $terms[] = $term->tid;
                    }
                }
            }
        }
    }
    if (!empty($terms)) {
        $all_terms = ctools_break_phrase(implode($conf['concatenator'], $terms));
        return ctools_context_create('terms', $all_terms);
    }
}