summaryrefslogtreecommitdiff
path: root/sites/all/modules/ctools/views_content/plugins/relationships/term_from_view.inc
blob: fc9e20d41c0d84e6b2b3e72d8fef5b067475579a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
<?php

/**
 * @file
 * Plugin to provide an relationship handler for term from view.
 */

/**
 * Plugins are described by creating a $plugin array which will be used
 * by the system that includes this file.
 */
$plugin = array(
  'title' => t('Term from view'),
  'keyword' => 'term',
  'description' => t('Extract a term context from a view context of the base type term.'),
  'required context' => new ctools_context_required(t('View'), 'view', array('base' => 'taxonomy_term_data')),
  'context' => 'views_content_term_from_view_context',
  'edit form' => 'views_content_term_from_view_settings_form',
  'edit form validate' => 'views_content_term_from_view_settings_form_validate',
  'defaults' => array('row' => 1),
);

/**
 * Return a new context based on an existing context.
 */
function views_content_term_from_view_context($context, $conf, $placeholder = FALSE) {
  // If unset it wants a generic, unfilled context, which is just NULL.
  if (empty($context->data) || $placeholder) {
    return ctools_context_create_empty('entity:taxonomy_term', NULL);
  }
  $view = views_content_context_get_view($context);
  // Ensure the view executes, but we don't need its output.
  views_content_context_get_output($context);

  $row = intval($conf['row']) - 1;
  if (isset($view->result[$row])) {
    $tid = $view->result[$row]->{$view->base_field};
    if ($tid) {
      $term = taxonomy_term_load($tid);
      return ctools_context_create('entity:taxonomy_term', $term);
    }
  }
  return ctools_context_create_empty('entity:taxonomy_term', NULL);
}

/**
 * Settings form for the relationship.
 */
function views_content_term_from_view_settings_form($form, &$form_state) {
  $conf = $form_state['conf'];
  $form['row'] = array(
    '#title' => t('Row number'),
    '#type' => 'textfield',
    '#default_value' => $conf['row'],
  );

  return $form;
}

function views_content_term_from_view_settings_form_validate($form, &$form_state) {
  if (intval($form_state['values']['row']) <= 0) {
    form_error($form['row'], t('Row number must be a positive integer value.'));
  }
}