0

I am working with a custom view handler that renders a comma separated list. This field is used throughout the site this way.

Now, I have a new view that needs to render the list using line breaks instead of commas. Is there a way to do this in the view settings?

I don't want to hard-code the new view name in the view handler code to render it differently. Do I have to create a views template to solve this?

  • I think that you should create a views template, if you edit with more info will be better to understand the situation – Adrian Cid Almaguer Dec 1 '15 at 4:00
1

You can use the options_form() method on your field handler to provide a setting. That setting could be a text field in which you enter separator to be used, , or <br> or whatever you need. Or that setting could be a select list that contained only , and <br> options.

Here is an example of an field handler that uses that method:

http://cgit.drupalcode.org/views/tree/modules/node/views_handler_field_node_link.inc?h=7.x-3.13

The key pieces are the options_form()

  function options_form(&$form, &$form_state) {
    $form['text'] = array(
      '#type' => 'textfield',
      '#title' => t('Text to display'),
      '#default_value' => $this->options['text'],
    );
    parent::options_form($form, $form_state);

    // The path is set by render_link function so don't allow to set it.
    $form['alter']['path'] = array('#access' => FALSE);
    $form['alter']['external'] = array('#access' => FALSE);
  }

and then the usage of the option

  $text = !empty($this->options['text']) ? $this->options['text'] : t('view');
| improve this answer | |
  • I understand where you are headed with this. I also see a "Separator" field under settings. How can I access that field from the handler? I feel that would be a more appropriate field to save the separator. – Rayhan Muktader Nov 30 '15 at 21:36
  • $this->options contains all of the values that are saved to options in options_form() – Steve Persch Nov 30 '15 at 21:40
  • +1 for pointing me in the right direction. I needed the option_definition method in conjunction with options_form. Otherwise the value does not get saved. – Rayhan Muktader Dec 8 '15 at 15:12
1

The solution is to create an additional options form field for your views field. Which is done using the options_form and options_definition methods. This will allow you save a value using the options form and access that value in the render method.

class my_handler_my_field extends views_handler_field {

  /**
   * {@inheritdoc}
   *
   * Perform any database or cache data retrieval here. In this example there is
   * none.
   */
  function query() {

  }

  /**
   * {@inheritdoc}
   *
   * Modify any end user views settings here. Debug $options to view the field
   * settings you can change.
   */
  function option_definition() {
    $options = parent::option_definition();
    $options['separator'] = array('default' => ', ');
    return $options;
  }

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

    $form['separator'] = array(
      '#type' => 'textfield',
      '#title' => t('My Field'),
      '#default_value' => $this->options['separator'],
    );

    // The path is set by render_link function so don't allow to set it.
    $form['alter']['path'] = array('#access' => FALSE);
    $form['alter']['external'] = array('#access' => FALSE);

    return $form;
  }

  /**
   * Render callback handler.
   *
   * Return the markup that will appear in the rendered field.
   */
  function render($values) {
    $separator = !empty($this->options['separator']) ? $this->options['separator'] : ', ';
    ......
    return $rendered_value_with_the_right_separator;
  }
}
| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.