diff --git a/uc_addresses.info b/uc_addresses.info index c3b2c7c..588265c 100644 --- a/uc_addresses.info +++ b/uc_addresses.info @@ -23,6 +23,9 @@ files[] = handlers/ubercart.handlers.inc files[] = handlers/uc_addresses.handlers.inc ; Views handlers +files[] = views/uc_addresses_handler_field_uc_addresses_link.inc +files[] = views/uc_addresses_handler_field_uc_addresses_link_edit.inc +files[] = views/uc_addresses_handler_field_uc_addresses_link_delete.inc files[] = views/uc_addresses_handler_filter_access.inc ; Test cases diff --git a/views/uc_addresses.views.inc b/views/uc_addresses.views.inc index 61f8b2f..bfea4c8 100644 --- a/views/uc_addresses.views.inc +++ b/views/uc_addresses.views.inc @@ -25,6 +25,9 @@ function uc_addresses_views_data() { 'help' => t('Addresses in the customers address book that can be referenced when placing an order.'), ); + // Declare entity type. + $data['uc_addresses']['table']['entity type'] = 'uc_addresses'; + // Declare each schema field. foreach ($schema['fields'] as $fieldname => $schema_field) { if (isset($address_fields[$fieldname])) { @@ -333,5 +336,28 @@ function uc_addresses_views_data() { ), ); + // View, edit and delete links. + $data['uc_addresses']['view'] = array( + 'field' => array( + 'title' => t('Link'), + 'help' => t('Provide a simple link to the address.'), + 'handler' => 'uc_addresses_handler_field_uc_addresses_link', + ), + ); + $data['uc_addresses']['edit'] = array( + 'field' => array( + 'title' => t('Edit link'), + 'help' => t('Provide a simple link to edit the address.'), + 'handler' => 'uc_addresses_handler_field_uc_addresses_link_edit', + ), + ); + $data['uc_addresses']['delete'] = array( + 'field' => array( + 'title' => t('Delete link'), + 'help' => t('Provide a simple link to delete the address.'), + 'handler' => 'uc_addresses_handler_field_uc_addresses_link_delete', + ), + ); + return $data; } diff --git a/views/uc_addresses_handler_field_uc_addresses_link.inc b/views/uc_addresses_handler_field_uc_addresses_link.inc new file mode 100644 index 0000000..50bc798 --- /dev/null +++ b/views/uc_addresses_handler_field_uc_addresses_link.inc @@ -0,0 +1,52 @@ + '', 'translatable' => TRUE); + return $options; + } + + 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); + } + + function render($values) { + if ($entity = $this->get_value($values)) { + return $this->render_link($entity, $values); + } + } + + /** + * Renders the link if the user may view the address. + */ + function render_link($entity, $values) { + if (uc_addresses_entity_access('view', $entity)) { + $uri = entity_uri('uc_addresses', $entity); + $this->options['alter']['make_link'] = TRUE; + $this->options['alter']['path'] = $uri['path']; + $text = !empty($this->options['text']) ? $this->options['text'] : t('view'); + return $text; + } + } +} diff --git a/views/uc_addresses_handler_field_uc_addresses_link_delete.inc b/views/uc_addresses_handler_field_uc_addresses_link_delete.inc new file mode 100644 index 0000000..62e3cb1 --- /dev/null +++ b/views/uc_addresses_handler_field_uc_addresses_link_delete.inc @@ -0,0 +1,35 @@ +options['alter']['make_link'] = TRUE; + $this->options['alter']['path'] = $uri['path'] . '/delete'; + $this->options['alter']['query'] = drupal_get_destination(); + + $text = !empty($this->options['text']) ? $this->options['text'] : t('delete'); + return $text; + } +} diff --git a/views/uc_addresses_handler_field_uc_addresses_link_edit.inc b/views/uc_addresses_handler_field_uc_addresses_link_edit.inc new file mode 100644 index 0000000..831d487 --- /dev/null +++ b/views/uc_addresses_handler_field_uc_addresses_link_edit.inc @@ -0,0 +1,32 @@ +options['alter']['make_link'] = TRUE; + $this->options['alter']['path'] = $uri['path'] . '/edit'; + $this->options['alter']['query'] = drupal_get_destination(); + + $text = !empty($this->options['text']) ? $this->options['text'] : t('edit'); + return $text; + } +}