You are here

class masquerade_handler_field_user in Masquerade Extras 6

Same name and namespace in other branches
  1. 6.2 masquerade_views/handlers/masquerade_handler_field_user.inc \masquerade_handler_field_user
  2. 7.2 masquerade_views/handlers/masquerade_handler_field_user.inc \masquerade_handler_field_user
  3. 7 masquerade_views/handlers/masquerade_handler_field_user.inc \masquerade_handler_field_user

Field handler that allows a link to be generated for a user so you may masquerade as them.

Hierarchy

Expanded class hierarchy of masquerade_handler_field_user

1 string reference to 'masquerade_handler_field_user'
masquerade_views_views_data in masquerade_views/masquerade_views.views.inc
Implements hook_views_data().

File

masquerade_views/handlers/masquerade_handler_field_user.inc, line 11
Views field handler.

View source
class masquerade_handler_field_user extends views_handler_field_user_link {

  /**
   * Set custom options and defaults on this handler.
   */
  function option_definition() {
    $options = parent::option_definition();
    $options['masquerade_views']['custom_destination'] = array(
      'default' => FALSE,
    );
    $options['masquerade_views']['destination'] = array(
      'default' => '',
    );
    return $options;
  }

  /**
   * Adds additional options to the Field options form
   */
  function options_form(&$form, &$form_state) {
    parent::options_form($form, $form_state);
    $form['masquerade_views'] = array(
      '#tree' => TRUE,
    );
    $form['masquerade_views']['custom_destination'] = array(
      '#title' => t('Provide a custom destination.'),
      '#description' => t('This is helpful for providing a shortcut to a certain function of the site from one area or another.'),
      '#type' => 'checkbox',
      '#default_value' => !empty($this->options['masquerade_views']['custom_destination']) ? $this->options['masquerade_views']['custom_destination'] : FALSE,
    );

    // Provide an optional redirect immediately after switching to another user
    $form['masquerade_views']['destination'] = array(
      '#title' => t('Alternate destination'),
      '#description' => t('Redirect the browser to this page immediately after switching to another user. There is a caveat, however. When you switch back, masquerade will not remember where you last were.'),
      '#type' => 'textfield',
      '#default_value' => !empty($this->options['masquerade_views']['destination']) && valid_url($this->options['masquerade_views']['destination']) ? $this
        ->sanitize_value($this->options['masquerade_views']['destination']) : '',
    );

    // Override the normal link-to title and description.
    $form['link_to_user']['#title'] = t('Generate a link to Masquerade as this user.');
    $form['link_to_user']['#description'] = t('Be careful not to rewrite this field with tokens that generate links. This field will only display a link to users who have permission to masquerade.');
  }

  /**
   * Prevent users that don't have permission to masquerade from accessing this function.
   */
  function access() {
    return user_access('masquerade as user');
  }

  /**
   * Renders this field.
   * @param array $values
   *  Values of real fields keyed by their db name.
   * @returns
   *  A link!
   * @retval string
   */
  function render($values) {
    return $this
      ->render_link($values);
  }

  /**
   * Updates the output configuration of this field.
   * Then returns the anchor text that will be used.
   * @param array $values
   *  Values of real fields keyed by their db name.
   * @returns
   *  Link text.
   * @retval string
   */
  function render_link($values = array()) {
    if (isset($values->{$this->aliases['uid']})) {

      // Make sure any data being used to generate the link is clean.
      // We removed $this->sanitize_value() since it performs similar functionality
      // and is only available in the 3.x branch of Views.
      $switchto = filter_xss($values->{$this->aliases['uid']});
      $this->options['alter']['make_link'] = TRUE;
      $this->options['alter']['path'] = "masquerade/switch/" . $switchto;

      // Use a security token to make the switch.
      $query = array(
        'token' => drupal_get_token($this->options['alter']['path']),
      );

      // If a custom destination was provided, we'll append it to the URL.
      if ($this->options['masquerade_views']['custom_destination'] == TRUE && !empty($this->options['masquerade_views']['destination'])) {
        $query['destination'] = $this->options['masquerade_views']['destination'];
      }
      $this->options['alter']['query'] = $this
        ->_build_query($query);
    }
    if (!empty($this->options['alter']['text'])) {
      $this->options['alter']['alter_text'] = 1;
    }
    return !empty($this->options['text']) ? $this->options['text'] : t('Masquerade');
  }

  /**
   * Creates a URL querystring from an array.
   * @note Views doesnt condense arrays for the query on our behalf.
   */
  function _build_query($components) {
    if (empty($components)) {
      return '';
    }
    $output = array();
    foreach ($components as $key => $value) {
      $output[] = "{$key}={$value}";
    }
    return implode('&', $output);
  }

}

Members

Namesort descending Modifiers Type Description Overrides
masquerade_handler_field_user::access function Prevent users that don't have permission to masquerade from accessing this function.
masquerade_handler_field_user::options_form function Adds additional options to the Field options form
masquerade_handler_field_user::option_definition function Set custom options and defaults on this handler.
masquerade_handler_field_user::render function Renders this field.
masquerade_handler_field_user::render_link function Updates the output configuration of this field. Then returns the anchor text that will be used.
masquerade_handler_field_user::_build_query function Creates a URL querystring from an array. @note Views doesnt condense arrays for the query on our behalf.