You are here

views_add_button_area.inc in Views Add Button 7

Definition of views_add_button_area.

File

views/views_add_button_area.inc
View source
<?php

/**
 * @file
 * Definition of views_add_button_area.
 */

/**
 * Views area text handler.
 *
 * @ingroup views_area_handlers
 */
class views_add_button_area extends views_handler_area {
  function option_definition() {
    $options = parent::option_definition();
    $options['content_type'] = array(
      'default' => '',
    );
    $options['button_classes'] = array(
      'default' => '',
    );
    $options['query_string'] = array(
      'default' => '',
    );
    $options['destination'] = array(
      'default' => TRUE,
    );
    $options['tokenize'] = array(
      'default' => FALSE,
      'bool' => TRUE,
    );
    return $options;
  }
  function options_form(&$form, &$form_state) {
    parent::options_form($form, $form_state);
    $ct_options = array();
    foreach (node_type_get_types() as $key => $type) {
      $ct_options[$key] = $type->name;
    }
    $form['content_type'] = array(
      '#type' => 'select',
      '#title' => t('Content Type'),
      '#options' => $ct_options,
      '#empty_option' => '- Select -',
      '#default_value' => $this->options['content_type'],
    );
    $form['query_string'] = array(
      '#type' => 'textfield',
      '#title' => t('Query string to append to the add link'),
      '#default_value' => $this->options['query_string'],
    );
    $form['button_classes'] = array(
      '#type' => 'textfield',
      '#title' => t('Button classes for the add link - usually "button" or "btn," with additional styling classes.'),
      '#default_value' => $this->options['button_classes'],
    );
    $form['destination'] = array(
      '#type' => 'checkbox',
      '#title' => t('Include destination parameter?'),
      '#default_value' => $this->options['destination'],
    );

    // @TODO: Refactor token handling into a base class.
    $form['tokenize'] = array(
      '#type' => 'checkbox',
      '#title' => t('Use replacement tokens from the first row (for the query string or button classes)'),
      '#default_value' => $this->options['tokenize'],
    );

    // Get a list of the available fields and arguments for token replacement.
    $options = array();
    foreach ($this->view->display_handler
      ->get_handlers('field') as $field => $handler) {
      $options[t('Fields')]["[{$field}]"] = $handler
        ->ui_name();
    }
    $count = 0;

    // This lets us prepare the key as we want it printed.
    foreach ($this->view->display_handler
      ->get_handlers('argument') as $arg => $handler) {
      $options[t('Arguments')]['%' . ++$count] = t('@argument title', array(
        '@argument' => $handler
          ->ui_name(),
      ));
      $options[t('Arguments')]['!' . $count] = t('@argument input', array(
        '@argument' => $handler
          ->ui_name(),
      ));
    }
    if (!empty($options)) {
      $output = '<p>' . t('The following tokens are available. If you would like to have the characters \'[\' and \']\' please use the html entity codes \'%5B\' or  \'%5D\' or they will get replaced with empty space.' . '</p>');
      foreach (array_keys($options) as $type) {
        if (!empty($options[$type])) {
          $items = array();
          foreach ($options[$type] as $key => $value) {
            $items[] = $key . ' == ' . check_plain($value);
          }
          $output .= theme('item_list', array(
            'items' => $items,
            'type' => $type,
          ));
        }
      }
      $form['token_help'] = array(
        '#type' => 'fieldset',
        '#title' => t('Replacement patterns'),
        '#collapsible' => TRUE,
        '#collapsed' => TRUE,
        '#value' => $output,
        '#id' => 'edit-options-token-help',
        '#dependency' => array(
          'edit-options-tokenize' => array(
            1,
          ),
        ),
        '#prefix' => '<div>',
        '#suffix' => '</div>',
      );
    }
  }
  function render($empty = FALSE) {
    if (!$empty || !empty($this->options['empty'])) {
      $content_type = check_plain($this->options['content_type']);
      if (user_access('create ' . $content_type . ' content') || user_access('bypass node access')) {
        $content_type_hyphen = str_replace('_', '-', $content_type);
        $ct_options = array();
        foreach (node_type_get_types() as $key => $type) {
          $ct_options[$key] = $type->name;
        }
        $query_string = filter_xss($this->options['query_string']);
        $button_classes = check_plain($this->options['button_classes']);
        if ($this->options['tokenize']) {
          $query_string = $this->view->style_plugin
            ->tokenize_value($query_string, 0);
          $button_classes = $this->view->style_plugin
            ->tokenize_value($button_classes, 0);
        }

        // Build the link
        $url = '/node/add/' . $content_type_hyphen;
        $url .= $query_string ? '?' . $query_string : '';
        if ($this->options['destination'] && strpos($url, 'destination') === FALSE) {
          $url .= $query_string ? '&destination=' . current_path() : '?destination=' . current_path();
        }
        $l = '<a href="' . $url . '" class="' . $button_classes . '">' . t('Add ' . $ct_options[$content_type]) . '</a>';
        return $l;
      }
      return '';
    }
    return '';
  }

}

Classes

Namesort descending Description
views_add_button_area Views area text handler.