Skip to content
Comment.php 5.97 KiB
Newer Older
Earl Miles's avatar
Earl Miles committed
<?php

/**
 * @file
 * Definition of Drupal\node\Plugin\views\wizard\Comment.
use Drupal\views\Plugin\views\wizard\WizardPluginBase;

/**
 * @todo: replace numbers with constants.
 */
Earl Miles's avatar
Earl Miles committed
/**
 * Tests creating comment views with the wizard.
Bram Goffings's avatar
Bram Goffings committed
 *   id = "comment",
 *   base_table = "comment",
 *   title = @Translation("Comments")
class Comment extends WizardPluginBase {
  /**
   * Set the created column.
   */
  protected $createdColumn = 'created';

  /**
   * Set default values for the path field options.
   */
  protected $pathField = array(
    'id' => 'cid',
    'table' => 'comment',
    'field' => 'cid',
    'exclude' => TRUE,
    'link_to_comment' => FALSE,
    'alter' => array(
      'alter_text' => TRUE,
      'text' => 'comment/[cid]#comment-[cid]'
    ),
  );

  /**
   * Set default values for the filters.
   */
  protected $filters = array(
    'status' => array(
      'value' => TRUE,
    ),
    'status_node' => array(
      'value' => TRUE,
   * Overrides Drupal\views\Plugin\views\wizard\WizardPluginBase::rowStyleOptions().
Earl Miles's avatar
Earl Miles committed
    $options = array();
    $options['comment'] = t('comments');
    $options['fields'] = t('fields');
    return $options;
  }

  protected function buildFormStyle(array &$form, array &$form_state, $type) {
    parent::buildFormStyle($form, $form_state, $type);
Earl Miles's avatar
Earl Miles committed
    $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 'comment':
        $style_form['row_options']['links'] = array(
          '#type' => 'select',
          '#title' => t('Should links be displayed below each comment'),
Earl Miles's avatar
Earl Miles committed
          '#options' => array(
            1 => t('with links (allow users to reply to the comment, etc.)'),
            0 => t('without links'),
          ),
          '#default_value' => 1,
        );
        break;
    }
  }

  protected function pageDisplayOptions(array $form, array &$form_state) {
    $display_options = parent::pageDisplayOptions($form, $form_state);
Earl Miles's avatar
Earl Miles committed
    $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;
  }

   * Overrides Drupal\views\Plugin\views\wizard\WizardPluginBase::blockDisplayOptions().
  protected function blockDisplayOptions(array $form, array &$form_state) {
    $display_options = parent::blockDisplayOptions($form, $form_state);
Earl Miles's avatar
Earl Miles committed
    $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 'comment':
        $display_options['row']['type'] = 'entity:comment';
        $display_options['row']['options']['links'] = !empty($row_options['links']);
   * Overrides Drupal\views\Plugin\views\wizard\WizardPluginBase::defaultDisplayOptions().
  protected function defaultDisplayOptions() {
    $display_options = parent::defaultDisplayOptions();
Earl Miles's avatar
Earl Miles committed

    // Add permission-based access control.
    $display_options['access']['type'] = 'perm';
    $display_options['access']['provider'] = 'user';
Earl Miles's avatar
Earl Miles committed

    // Add a relationship to nodes.
    $display_options['relationships']['node']['id'] = 'node';
    $display_options['relationships']['node']['table'] = 'comment_field_data';
    $display_options['relationships']['node']['field'] = 'node';
    $display_options['relationships']['node']['required'] = 1;
    $display_options['relationships']['node']['plugin_id'] = 'standard';
    $display_options['relationships']['node']['provider'] = 'views';
Earl Miles's avatar
Earl Miles committed

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

    /* Field: Comment: Title */
    $display_options['fields']['subject']['id'] = 'subject';
    $display_options['fields']['subject']['table'] = 'comment_field_data';
Earl Miles's avatar
Earl Miles committed
    $display_options['fields']['subject']['field'] = 'subject';
    $display_options['fields']['subject']['provider'] = 'comment';
Earl Miles's avatar
Earl Miles committed
    $display_options['fields']['subject']['label'] = '';
    $display_options['fields']['subject']['alter']['alter_text'] = 0;
    $display_options['fields']['subject']['alter']['make_link'] = 0;
    $display_options['fields']['subject']['alter']['absolute'] = 0;
    $display_options['fields']['subject']['alter']['trim'] = 0;
    $display_options['fields']['subject']['alter']['word_boundary'] = 0;
    $display_options['fields']['subject']['alter']['ellipsis'] = 0;
    $display_options['fields']['subject']['alter']['strip_tags'] = 0;
    $display_options['fields']['subject']['alter']['html'] = 0;
    $display_options['fields']['subject']['hide_empty'] = 0;
    $display_options['fields']['subject']['empty_zero'] = 0;
    $display_options['fields']['subject']['link_to_comment'] = 1;

    return $display_options;
  }