You are here

class uc_addresses_handler_field_uc_addresses_link in Ubercart Addresses 6.2

Same name and namespace in other branches
  1. 7 views/uc_addresses_handler_field_uc_addresses_link.inc \uc_addresses_handler_field_uc_addresses_link

Field handler to present a link to the address.

Hierarchy

Expanded class hierarchy of uc_addresses_handler_field_uc_addresses_link

2 string references to 'uc_addresses_handler_field_uc_addresses_link'
uc_addresses_views_data in views/uc_addresses.views.inc
Implementation of hook_views_data().
uc_addresses_views_handlers in views/uc_addresses.views.inc
Implementation of hook_views_handlers().

File

views/uc_addresses_handler_field_uc_addresses_link.inc, line 13
Definition of uc_addresses_handler_field_uc_addresses_link.

View source
class uc_addresses_handler_field_uc_addresses_link extends views_handler_field {

  /**
   * Overrides views_handler_field#construct().
   *
   * Adds fields to the query that are needed to load an address record.
   */
  function construct() {
    parent::construct();
    $this->additional_fields['aid'] = 'aid';
    $this->additional_fields['uid'] = 'uid';
  }

  /**
   * Implements views_handler_field#option_definition().
   */
  function option_definition() {
    $options = parent::option_definition();
    $options['text'] = array(
      'default' => '',
      'translatable' => TRUE,
    );
    return $options;
  }

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

  /**
   * Overrides views_handler_field#query().
   *
   * Ensures no "real" field is added to the query.
   */
  function query() {
    $this
      ->ensure_my_table();
    $this
      ->add_additional_fields();
  }

  /**
   * Implements views_handler_field#render().
   */
  function render($values) {
    $aid = $values->{$this->aliases['aid']};
    $uid = $values->{$this->aliases['uid']};
    $address = UcAddressesAddressBook::get($uid)
      ->getAddressById($aid);
    if ($address) {
      return $this
        ->uc_addresses_render_link($address);
    }
  }

  /**
   * Renders the link if the user may view the address.
   *
   * @param UcAddressesAddress $address
   *   An address object.
   *
   * @return string
   *   The rendered link, if the current user has access.
   */
  function uc_addresses_render_link(UcAddressesAddress $address) {
    $address_user = user_load($address
      ->getUserId());
    if (UcAddressesPermissions::canViewAddress($address_user, $address)) {
      $uri = $address
        ->uri();
      $this->options['alter']['make_link'] = TRUE;
      $this->options['alter']['path'] = $uri['path'];
      $text = !empty($this->options['text']) ? $this->options['text'] : t('view');
      return $text;
    }
  }

}

Members

Namesort descending Modifiers Type Description Overrides
uc_addresses_handler_field_uc_addresses_link::construct function Overrides views_handler_field#construct().
uc_addresses_handler_field_uc_addresses_link::options_form function Implements views_handler_field#options_form().
uc_addresses_handler_field_uc_addresses_link::option_definition function Implements views_handler_field#option_definition().
uc_addresses_handler_field_uc_addresses_link::query function Overrides views_handler_field#query().
uc_addresses_handler_field_uc_addresses_link::render function Implements views_handler_field#render().
uc_addresses_handler_field_uc_addresses_link::uc_addresses_render_link function Renders the link if the user may view the address. 2