core.inc 3.03 KB
Newer Older
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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
<?php
// $Id$

/**
 * Add a display handler that will use the Drupal core method of display.
 */
class TaxonomyDisplayAssociatedDisplayHandlerCore extends TaxonomyDisplayAssociatedDisplayHandler {
  /**
   * Build our output to be rendered to the user.
   *
   * @see TaxonomyDisplayAssociatedDisplayHandler::displayAssociated()
   */
  public function displayAssociated($term, $options = NULL) {
    $build = array();

    $number_nodes = isset($options['associated_per_page']) ? $options['associated_per_page'] : variable_get('default_nodes_main', 10);
    if ($nids = taxonomy_select_nodes($term->tid, TRUE, $number_nodes)) {
      $nodes = node_load_multiple($nids);
      $build += node_view_multiple($nodes);
      $build['pager'] = array(
        '#theme' => 'pager',
        '#weight' => 5,
       );
    }
    elseif (isset($options['empty_text']) && !empty($options['empty_text'])) {
      $build['no_content'] = array(
        '#prefix' => '<p>',
        '#markup' => t($options['empty_text']),
        '#suffix' => '</p>',
      );
    }

    return $build;
  }

  /**
   * Build our form for the fieldset.
   *
   * @see TaxonomyDisplayHandlerForm::formFieldset()
   */
  public function formFieldset(&$form, &$values, $options = NULL) {
    $form['#description'] = t('The core Drupal functionality will be used to display the term\'s associated content.');

    $form['associated_per_page'] = array(
      '#default_value' => isset($options['associated_per_page']) ? $options['associated_per_page'] : variable_get('default_nodes_main', 10),
      '#description' => t('Enter 0 to display all associated content for the taxonomy term.'),
      '#maxlength' => 7,
      '#required' => TRUE,
      '#size' => 8,
      '#title' => t('Number of associated content entities per page'),
      '#type' => 'textfield',
    );

    $form['empty_text'] = array(
      '#default_value' => isset($options['empty_text']) ? $options['empty_text'] : 'There is currently no content classified with this term.',
      '#description' => t('Text displayed if there is no associated content, set to blank if no empty text should be displayed.'),
      '#title' => t('No associated content text'),
      '#type' => 'textfield',
    );
  }

  /**
   * We store values to access later for rendering and editing.
   *
   * @see TaxonomyDisplayHandlerForm::formSubmit()
   */
  public function formSubmit($form, &$values) {
    // We are using the exact keys that our formFieldset() implementation
    // defines and we want all of the values stored, so we have no need to alter
    // them before returning.
    return $values;
  }

  /**
   * Validate the form values.
   *
   * @see TaxonomyDisplayHandlerForm::formValidate()
   */
  public function formValidate($form, &$values) {
    if ($values['associated_per_page'] !== '0' &&
        !preg_match('#^[1-9][0-9]*$#', $values['associated_per_page'])) {
      form_set_error(implode('][', $form['associated_per_page']['#parents']), t('The number of associated content entities per page must be a whole, non-negative number.'));
    }

    return $this;
  }
}