Node.php 6.19 KB
Newer Older
merlinofchaos's avatar
merlinofchaos committed
1 2 3 4
<?php

/**
 * @file
5
 * Definition of Drupal\views\Plugin\views\wizard\Node.
merlinofchaos's avatar
merlinofchaos committed
6 7
 */

8
namespace Drupal\views\Plugin\views\wizard;
9

10
use Drupal\views\Plugin\views\wizard\WizardPluginBase;
11 12 13 14 15 16
use Drupal\Core\Annotation\Plugin;
use Drupal\Core\Annotation\Translation;

/**
 * @todo: replace numbers with constants.
 */
17

merlinofchaos's avatar
merlinofchaos committed
18 19
/**
 * Tests creating node views with the wizard.
20 21
 *
 * @Plugin(
aspilicious's avatar
aspilicious committed
22
 *   id = "node",
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
 *   base_table = "node",
 *   created_column = "created",
 *   title = @Translation("Content"),
 *   available_sorts = {
 *     "title:DESC" = @Translation("Title")
 *   },
 *   filters = {
 *     "status" = {
 *       "value" = 1,
 *       "table" = "node",
 *       "field" = "status"
 *     }
 *   },
 *   path_field = {
 *     "id" = "nid",
 *     "table" = "node",
 *     "field" = "nid",
 *     "exclude" = TRUE,
 *     "link_to_node" = FALSE,
 *     "alter" = {
 *       "alter_text" = 1,
 *       "text" = "node/[nid]"
 *     }
 *   }
 * )
 *
merlinofchaos's avatar
merlinofchaos committed
49
 */
50

51
class Node extends WizardPluginBase {
merlinofchaos's avatar
merlinofchaos committed
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 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176

  protected function row_style_options($type) {
    $options = array();
    $options['teasers'] = t('teasers');
    $options['full_posts'] = t('full posts');
    $options['titles'] = t('titles');
    $options['titles_linked'] = t('titles (linked)');
    $options['fields'] = t('fields');
    return $options;
  }

  protected function build_form_style(&$form, &$form_state, $type) {
    parent::build_form_style($form, $form_state, $type);
    $style_form =& $form['displays'][$type]['options']['style'];
    // Some style plugins don't support row plugins so stop here if that's the
    // case.
    if (!isset($style_form['row_plugin']['#default_value'])) {
      return;
    }
    $row_plugin = $style_form['row_plugin']['#default_value'];
    switch ($row_plugin) {
      case 'full_posts':
      case 'teasers':
        $style_form['row_options']['links'] = array(
          '#type' => 'select',
          '#title_display' => 'invisible',
          '#title' => t('Should links be displayed below each node'),
          '#options' => array(
            1 => t('with links (allow users to add comments, etc.)'),
            0 => t('without links'),
          ),
          '#default_value' => 1,
        );
        $style_form['row_options']['comments'] = array(
          '#type' => 'select',
          '#title_display' => 'invisible',
          '#title' => t('Should comments be displayed below each node'),
          '#options' => array(
            1 => t('with comments'),
            0 => t('without comments'),
          ),
          '#default_value' => 0,
        );
        break;
    }
  }

  /**
   * @override
   */
  protected function default_display_options($form, $form_state) {
    $display_options = parent::default_display_options($form, $form_state);

    // Add permission-based access control.
    $display_options['access']['type'] = 'perm';

    // Remove the default fields, since we are customizing them here.
    unset($display_options['fields']);

    // Add the title field, so that the display has content if the user switches
    // to a row style that uses fields.
    /* Field: Content: Title */
    $display_options['fields']['title']['id'] = 'title';
    $display_options['fields']['title']['table'] = 'node';
    $display_options['fields']['title']['field'] = 'title';
    $display_options['fields']['title']['label'] = '';
    $display_options['fields']['title']['alter']['alter_text'] = 0;
    $display_options['fields']['title']['alter']['make_link'] = 0;
    $display_options['fields']['title']['alter']['absolute'] = 0;
    $display_options['fields']['title']['alter']['trim'] = 0;
    $display_options['fields']['title']['alter']['word_boundary'] = 0;
    $display_options['fields']['title']['alter']['ellipsis'] = 0;
    $display_options['fields']['title']['alter']['strip_tags'] = 0;
    $display_options['fields']['title']['alter']['html'] = 0;
    $display_options['fields']['title']['hide_empty'] = 0;
    $display_options['fields']['title']['empty_zero'] = 0;
    $display_options['fields']['title']['link_to_node'] = 1;

    return $display_options;
  }

  protected function page_display_options($form, $form_state) {
    $display_options = parent::page_display_options($form, $form_state);
    $row_plugin = isset($form_state['values']['page']['style']['row_plugin']) ? $form_state['values']['page']['style']['row_plugin'] : NULL;
    $row_options = isset($form_state['values']['page']['style']['row_options']) ? $form_state['values']['page']['style']['row_options'] : array();
    $this->display_options_row($display_options, $row_plugin, $row_options);
    return $display_options;
  }

  protected function block_display_options($form, $form_state) {
    $display_options = parent::block_display_options($form, $form_state);
    $row_plugin = isset($form_state['values']['block']['style']['row_plugin']) ? $form_state['values']['block']['style']['row_plugin'] : NULL;
    $row_options = isset($form_state['values']['block']['style']['row_options']) ? $form_state['values']['block']['style']['row_options'] : array();
    $this->display_options_row($display_options, $row_plugin, $row_options);
    return $display_options;
  }

  /**
   * Set the row style and row style plugins to the display_options.
   */
  protected  function display_options_row(&$display_options, $row_plugin, $row_options) {
    switch ($row_plugin) {
      case 'full_posts':
        $display_options['row_plugin'] = 'node';
        $display_options['row_options']['build_mode'] = 'full';
        $display_options['row_options']['links'] = !empty($row_options['links']);
        $display_options['row_options']['comments'] = !empty($row_options['comments']);
        break;
      case 'teasers':
        $display_options['row_plugin'] = 'node';
        $display_options['row_options']['build_mode'] = 'teaser';
        $display_options['row_options']['links'] = !empty($row_options['links']);
        $display_options['row_options']['comments'] = !empty($row_options['comments']);
        break;
      case 'titles_linked':
        $display_options['row_plugin'] = 'fields';
        $display_options['field']['title']['link_to_node'] = 1;
        break;
      case 'titles':
        $display_options['row_plugin'] = 'fields';
        $display_options['field']['title']['link_to_node'] = 0;
        break;
    }
  }
}