Thanks to visit codestin.com
Credit goes to git.drupalcode.org

Commit 460bf124 authored by Steven Wichers's avatar Steven Wichers Committed by Mathew Winstone
Browse files

Issue #1650012 by steven.wichers: More attributes, correct class attribute...

Issue #1650012 by steven.wichers: More attributes, correct class attribute usage, and real views token support
parent ef9c00a8
Loading
Loading
Loading
Loading
+166 −41
Original line number Diff line number Diff line
@@ -9,6 +9,11 @@ class views_handler_area_link extends views_handler_area {
    $options['querystring'] = array('default' => '');
    $options['anchor'] = array('default' => '');
    $options['class'] = array('default' => '');
    $options['title'] = array('default' => '');
    $options['rel'] = array('default' => '');
    $options['target'] = array('default' => '');
    $options['tokenize'] = array('default' => FALSE);

    return $options;
  }

@@ -38,27 +43,95 @@ class views_handler_area_link extends views_handler_area {
      '#default_value' => $this->options['anchor'],
      '#description' => t('The anchor data that follows the full path and query parameters'),
    );

    $form['attributes'] = array(
      '#type' => 'fieldset',
      '#title' => t('Attributes'),
      '#collapsible' => TRUE,
      '#collapsed' => TRUE,
    );

    $form['class'] = array(
      '#type' => 'textfield',
      '#title' => t('Link CSS class'),
      '#default_value' => $this->options['class'],
      '#description' => t('A custom CSS class to add to the link'),
      '#fieldset' => 'attributes',
    );

    $form['title'] = array(
      '#type' => 'textfield',
      '#title' => t('Link title'),
      '#description' => t('Set the title attribute of the link'),
      '#default_value' => $this->options['title'],
      '#fieldset' => 'attributes',
    );

    $form['rel'] = array(
      '#type' => 'textfield',
      '#title' => t('Link rel'),
      '#description' => t('Set the rel attribute of the link'),
      '#default_value' => $this->options['rel'],
      '#fieldset' => 'attributes',
    );

    $target_options = array(
      ''        => t('None'),
      '_blank'  => t('New window (_blank)'),
      '_parent' => '_parent',
      '_self'   => '_self',
      '_top'    => '_top',
    );

    $form['target'] = array(
      '#type' => 'select',
      '#title' => t('Link target'),
      '#description' => t('Set the target attribute of the link.'),
      '#options' => $target_options,
      '#default_value' => $this->options['target'],
      '#fieldset' => 'attributes',
    );

    $tokenize_id = drupal_html_id('tokenize_checkbox');
    $form['tokenize'] = array(
      '#id' => $tokenize_id,
      '#type' => 'checkbox',
      '#title' => t('Use replacement tokens from the first row'),
      '#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()));
    }

    $output = t('<p>You must add some additional fields to this display before using this field. These fields may be marked as <em>Exclude from display</em> if you prefer. Note that due to rendering order, you cannot use fields that come after this field; if you need a field not listed here, rearrange your fields.</p>');

    if (!empty($options)) {
      $output = t('<p>The following substitution patterns are available for this display based on the arguments for this view. Use the pattern shown on the left to display the value indicated on the right.</p>');
      foreach (array_keys($options) as $type) {
        if (!empty($options[$type])) {

      $output = '';

      foreach ($options as $type => $values) {

        if (empty($values)) {

          continue;
        }

        $vars = array('title' => $type);
          foreach ($options[$type] as $key => $value) {
            $vars['items'][] = $key . ' == ' . $value;
        foreach ($values as $key => $label) {

          $vars['items'][] = $key . ' == ' . $label;
        }

        $output .= theme('item_list', $vars);
      }
    }
@@ -66,20 +139,26 @@ class views_handler_area_link extends views_handler_area {
    $form['help'] = array(
      '#type' => 'fieldset',
      '#title' => t('Replacement patterns'),
      );
      $form['help']['help'] = array(
        '#type' => 'markup',
        '#markup' => $output,
      '#description' => '<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>',
      '#collapsible' => TRUE,
      '#collapsed' => TRUE,
      '#value' => $output,
      '#dependency' => array(
        $tokenize_id => array(1),
      ),
    );
  }
  }

  function options_submit(&$form, &$form_state) {
    parent::options_submit($form, $form_state);
  }

  function render($empty = FALSE) {
    if (!$empty || !empty($this->options['empty'])) {

    if ($empty && empty($this->options['empty'])) {

      return '';
    }

    $tokens = $this->get_render_tokens();

@@ -94,25 +173,71 @@ class views_handler_area_link extends views_handler_area {
      return '';
    }

      $link_options = array();
    // Where we store tokenized values.
    $link_properties = array();
    // Options that will go into the attributes array for url().
    $attribute_keys = array('title', 'target', 'rel', 'class');
    // Other options that we need to put into link properties.
    $option_keys = array_merge(array('anchor', 'text', 'path'), $attribute_keys);

    if (!empty($this->options['querystring'])) {
      // This is an ugly way to do it, but Drupal 7 now takes an array for
      // query instead of a string.  That's good, but makes our string field
      // not work.  This should get switched to a multi-value interface of
      // some kind instead of ugly string parsing. @todo
      $querystring = strtr($this->options['querystring'], $tokens);
        $link_options['query'] = drupal_get_query_array($querystring);
      $link_properties['query'] = drupal_get_query_array($querystring);
    }
      if (!empty($this->options['anchor'])) {
        $link_options['anchor'] = strtr($this->options['anchor'], $tokens);

    // Grab all of our options and tokenize them if necessary.
    foreach ($option_keys as $key) {

      if (empty($this->options[$key])) {

        continue;
      }
      if (!empty($this->options['class'])) {
        $link_options['attributes'] = array('class' => explode(' ', strtr($this->options['class'], $tokens)));

      $link_properties[$key] = $this->options[$key];

      // Apply the argument substitutions.
      if (!empty($tokens)) {

        $link_properties[$key] = str_replace(array_keys($tokens), array_values($tokens), $link_properties[$key]);
      }

      return l(strtr($this->options['text'], $tokens), $path, $link_options);
      // Apply the more advanced tokenization.
      if ($this->options['tokenize']) {

        $link_properties[$key] = $this->view->style_plugin->tokenize_value($link_properties[$key], 0);
      }
    return '';
    }

    if (empty($link_properties['attributes'])) {

      $link_properties['attributes'] = array();
    }

    // Move our attributes into an attribute array for ease of use with url().
    foreach ($attribute_keys as $key) {

      if (!empty($link_properties[$key])) {

        $link_properties['attributes'][$key] = $link_properties[$key];

        if ('class' === $key) {

          $link_properties['attributes'][$key] = explode(' ', $link_properties['attributes'][$key]);
        }
      }
    }

    // Make sure all HTML entities are decoded before passing to l().
    while (decode_entities($link_properties['text']) != $link_properties['text']) {

      $link_properties['text'] = decode_entities($link_properties['text']);
    }

    return l($link_properties['text'], $link_properties['path'], $link_properties);
  }

  /**