You are here

uc_addresses_handler_field_uc_addresses_link.inc in Ubercart Addresses 6.2

Same filename and directory in other branches
  1. 7 views/uc_addresses_handler_field_uc_addresses_link.inc

Definition of uc_addresses_handler_field_uc_addresses_link.

File

views/uc_addresses_handler_field_uc_addresses_link.inc
View source
<?php

/**
 * @file
 * Definition of uc_addresses_handler_field_uc_addresses_link.
 */

/**
 * Field handler to present a link to the address.
 *
 * @ingroup views_field_handlers
 */
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;
    }
  }

}

Classes

Namesort descending Description
uc_addresses_handler_field_uc_addresses_link Field handler to present a link to the address.