Support relationships?

Created on 18 October 2021, over 3 years ago
Updated 10 March 2023, almost 2 years ago

Problem/Motivation

VBO works on the "base id" of the Views' rows. Wondering how difficult it would be to support relationships in VBO. Such that if you had a relationship to some other entity, vbo could select that relationship as many other fields do and operate on that entity instead.

Yes, it is likely possible to redo the View to be based of that entity and then do all the reverse relationships to get back to the more natural views fields.. but, simply doing the more natural view and having one relationship to set for vbo seems like it would be a lot cleaner.

Steps to reproduce

Proposed resolution

Remaining tasks

User interface changes

API changes

Data model changes

Feature request
Status

Active

Version

4.0

Component

Core

Created by

🇨🇦Canada liquidcms

Live updates comments and jobs are added and updated live.
Sign in to follow issues

Comments & Activities

Not all content is available!

It's likely this issue predates Contrib.social: some issue and comment data are missing.

  • 🇩🇰Denmark Birk

    I have a view I can't "reverse" as well, has anyone had any luck getting VBO working with relationships?

    (A hack would work for me as well)

  • 🇵🇱Poland Graber

    Damn, it used to work with relationships before the base_id was introduced. Didn't realize that was broken then and only now noticed this issue. We'd have to map base_id from bulk form keys to the correct entity_id using the relationship in the action processor.

    I'm afraid nobody will take the workload to implement it though looking at the issue activity..

    If someone really needs a feature that doesn't have many followers I guess the only option is to sponsor the development or just do it themselves.

  • 🇩🇰Denmark Birk

    I've been looking into this a bit, and it seems relationships is almost supported (and I'm sorry if my observations below is all over the place, I'm just brainstorming out loud).

    Like Graber stated the relationship needs to map to the correct keys, which is here: https://git.drupalcode.org/project/views_bulk_operations/-/blob/7a991c4c...

        $base_field_values = [];
        foreach ($batch_list as $item) {
          $base_field_values[$item[0]] = $item[0];
        }
    

    The item[0] is the base ID, but the seems the ViewsBulkOperationsViewData::getEntity() already supports relationship and gets the correct entity, it's inserted in $item[3].

    So by simply changing the $base_field_alias to the field alias of the relationship it should work.

    I'm making the assumption that the table alias is [relationship base]_[relationship_id], I'm doing this because I can't find a reliable way of getting the table from a relationship. So if I rewrite the code in ViewsBulkOperationsActionProcessor::popuplateQueue() to this:

    if (empty($this->bulkFormData['relationship_id'])) {
          if (isset($this->view->query->fields[$base_field])) {
            if (!empty($this->view->query->fields[$base_field]['table'])) {
              $base_field_alias = $this->view->query->fields[$base_field]['table'] . '.' . $this->view->query->fields[$base_field]['alias'];
            }
            else {
              $base_field_alias = $this->view->query->fields[$base_field]['alias'];
            }
          }
          else {
            $base_field_alias = $base_field;
          }
        }
        else {
          $viewsData = Views::viewsData();
    
          $relationship = $this->view->getDisplay()->getOption('relationships')[$this->bulkFormData['relationship_id']];
          $table_data = $viewsData->get($relationship['table']);
    
          $relationship_table_alias = $table_data[$relationship['field']]['relationship']['base'] . '_' . $relationship['table'];
          $base_field_alias = $relationship_table_alias . '.' . $table_data[$relationship['field']]['relationship']['base field'];
        }
    

    It seems to work in my particular case.

    I know this is a hot mess, it's a hack that did it for my use case. Maybe a proper fix is not that far away, the current project I'm working on just don't allow me to spend the time I need to dig any deeper.

  • 🇦🇺Australia sime Melbourne

    I was wondering why my user action was not appearing when it there was clearly a base user table in my view through a relationship. Now I have an action that must have no "type" in order to be available to add to bulk operations, and the action itself must go through hoops to work out how to get to the entity IDs it wants to operate on.

    I'd like to call this a serious bug.

  • 🇵🇱Poland nsavitsky

    Just as other people here I got stucked with a View where I can't revert relationships. Looking at the code of the VBO's view field plugin, it looks like it does fully support relationships under the hood. The problem is that in administration interface we don't have a Relationship dropdown. I manually edited view configuration file and replaced relationship: none with needed relationship and... it works! Now I'm getting operations over entities from relationship I provided.

    Probably, problem is not very deep and can be fixed quickly. Unfortunately, I'm not an expert in views plugins and I don't have enough free time to investigate why relationship dropdown is missing. For now, as a workaround for struggling devs I recommend to try to edit view configuration just like I did. Beware that editing view in administration area will reset relationship you added manually!

Production build 0.71.5 2024