Use hook_views_data() instead of hook_views_data_alter() to allow alters in custom modules.

CommentFileSizeAuthor
#2 use_hook_views_data-2697287-2.patch707 bytesjoelpittet

Comments

joelpittet created an issue. See original summary.

joelpittet’s picture

Status: Active » Needs review
StatusFileSize
new707 bytes

Here's a patch with that change, it seems pretty straight forward.

jibran’s picture

Status: Needs review » Reviewed & tested by the community

Seems reasonable.

damiankloip’s picture

Status: Reviewed & tested by the community » Needs review

This is purposely in the alter hook as we are adding to an existing table technically, not declaring a new one. That has been our general rule of thumb for views data. What's the use case for wanting to alter this?

joelpittet’s picture

@damiankloip I needed to force one field to be clickable for a specific display (datatables).

function custom_views_data_alter(&$data) {
  // Allow the views fields to be sortable.
  $data['views']['view']['field']['click sortable'] = TRUE;
}
damiankloip’s picture

Click sortable should actually be disabled, it is in the 8.x branch (did that in the port, just never got round to doing it in the 7.x branch). This is essentially a computed field, so how does click sorting even work?

joelpittet’s picture

There are a number of js libraries for sorting tables that avoid the Ajax and page loads if you have a fixed set of results.

The one I'm using specifically is https://www.drupal.org/project/datatables

Essentially allowing any column to be sortable on the js side.

damiankloip’s picture

AH ha, sorry. I totally missed you said datatables. Yes, I've used that a fair bit (not with views).

This is not a common case, so a hook_module_implements_alter() and a hook_views_data_alter() will work here. I kind of don't want to change this, but I see your problem now.

damiankloip’s picture

You could also do something in an hook_preprocess_views_view_table() to add the label.

joelpittet’s picture

@damiankloip I don't understand your point in #4 and why this doesn't seem as straight forward as allowing other modules to alter the definition provided by the "non-alter" hook. (in my mind the "definition" hook). Leaving the alter hook for the exceptions in which would need to alter.

Otherwise I'd need to write the hook_module_implements_alter to alter the order/weight of the alter hook executions which just feels wrong...

damiankloip’s picture

The point is usually views_data is for declaring new tables and views_data_alter is for adding to/altering existing tables. Global is a special case but still a table.

damiankloip’s picture

Anyway, we can probably just do it. I just don't like the fact that we have to just so a hack can work 'cleanly' ;)

joelpittet’s picture

Clean hacks FTW!

tim.plunkett’s picture

Status: Needs review » Reviewed & tested by the community

The proposed approach is not architecturally pure, but it works and avoids hook_module_implements_alter.
RTBC from me.

vladimiraus’s picture

Status: Reviewed & tested by the community » Closed (outdated)

Thank you for your contributions.
Drupal 7 is no longer supported.
Closing this issue as outdated.

Now that this issue is closed, review the contribution record.

As a contributor, attribute any organization that helped you, or if you volunteered your own time.

Maintainers, credit people who helped resolve this issue.