Problem/Motivation

commerce_product_handler_field_product_link does not use entity_uri(), but constructs entity URLs itself, completely bypassing any other module's abilities to alter the URL.

Proposed resolution

Make the Views field handler use entity_uri() instead of constructing URls itself.

Remaining tasks

None.

User interface changes

No changes, but additions. The field now extends Views' URL field base class, rather than the generic field base classes.

API changes

None.

Data model changes

CommentFileSizeAuthor
#2 commerce_2604290_2.patch1.39 KBXano
Support from Acquia helps fund testing for Drupal Acquia logo

Comments

Xano created an issue. See original summary.

Xano’s picture

Assigned: Xano » Unassigned
Status: Active » Needs review
FileSize
1.39 KB
mglaman’s picture

Issue tags: +commerce-sprint
joelpittet’s picture

Status: Needs review » Needs work
+++ b/modules/product/includes/views/handlers/commerce_product_handler_field_product_link.inc
@@ -3,7 +3,7 @@
+class commerce_product_handler_field_product_link extends views_handler_field_url {

@@ -33,10 +33,18 @@ class commerce_product_handler_field_product_link extends views_handler_field {
-    return l($text, 'admin/commerce/products/' . $product_id);
+    return url($uri['path'], $uri['options']);

I could be wrong but shouldn't this be a new handler called commerce_product_handler_field_product_url? Before it was returning a link tag and now it's returning a URL. Something is not fitting.

mglaman’s picture

Status: Needs work » Needs review
+++ b/modules/product/includes/views/handlers/commerce_product_handler_field_product_link.inc
@@ -33,10 +33,18 @@ class commerce_product_handler_field_product_link extends views_handler_field {
+  function get_value($values, $field = NULL) {

Changed from render to get_value, because views_handler_field_url renders a link.

  /**
   * Render the field.
   *
   * @param $values
   *   The values retrieved from the database.
   */
  public function render($values) {
    return EntityFieldHandlerHelper::render($this, $values);
  }

Drills into

  public static function render_entity_link($handler, $value, $values) {
    // Allow easy overriding of this behaviour in the specific field handler.
    if (method_exists($handler, 'render_entity_link')) {
      return $handler->render_entity_link($value, $values);
    }
    $render = self::render_single_value($handler, $value, $values);
    if (!$handler->options['link_to_entity']) {
      return $render;
    }
    $entity = $handler->get_value($values, 'entity object');
    if (is_object($entity) && ($url = entity_uri($handler->entity_type, $entity))) {
      return l($render, $url['path'], array('html' => TRUE) + $url['options']);
    }
    return $render;
  }

Now it uses Entity API to properly render link

Xano’s picture

@mglaman: Did you accidentally forgot to attach a patch?

mglaman’s picture

Xano, I moved it back to CNR for your patch as it's proper in my opinion.

joelpittet’s picture

Status: Needs review » Needs work

@mglaman if views_handler_field_url renders a link, and this is being changed to use that class, shouldn't this also render a link as it did before then? Either the name is really confusing or I am missing something...

joelpittet’s picture

To clarify my concern the render() to get_value() override looks fine, my only concern is the confusing class name.

joelpittet’s picture

Maybe we can't do anything about that but yeah...

Xano’s picture

It looks like you're right. I just haven't had the chance to look at this code again.