Problem/Motivation

Bulk form is being added with hook_views_data_alter when it's added to the table of the entity defining it, i.e.

/**
 * Implements hook_views_data_alter().
 */
function node_views_data_alter(&$data) {
  $data['node']['node_bulk_form'] = array(
    'title' => t('Node operations bulk form'),
    'help' => t('Add a form element that lets you run operations on multiple nodes.'),
    'field' => array(
      'id' => 'node_bulk_form',
    ),
  );
}

Discussed a little for the comments integration in #1986606-157: Convert the comments administration screen to a view

This belongs to the corresponding EntityViewsData.

Proposed resolution

Remove the definition from hook_views_data_alter and move it to the EntityViewsData. Only node and user are affected by this.

API changes

CommentFileSizeAuthor
#1 2400153-views_data.patch2.61 KBpcambra
Support from Acquia helps fund testing for Drupal Acquia logo

Comments

pcambra’s picture

Status: Active » Needs review
Related issues: +#1986606: Convert the comments administration screen to a view
FileSize
2.61 KB

Status: Needs review » Needs work

The last submitted patch, 1: 2400153-views_data.patch, failed testing.

pcambra’s picture

Status: Needs work » Needs review
Issue tags: +Quick fix
jibran’s picture

Status: Needs review » Reviewed & tested by the community

Perfect! thanks for the patch and issue.

dawehner’s picture

+1

+++ b/core/modules/node/src/NodeViewsData.php
@@ -115,6 +115,14 @@ public function getViewsData() {
+    $data['node']['node_bulk_form'] = array(
+      'title' => t('Node operations bulk form'),
+      'help' => t('Add a form element that lets you run operations on multiple nodes.'),
+      'field' => array(
+        'id' => 'node_bulk_form',
+      ),
+    );

+++ b/core/modules/user/src/UserViewsData.php
@@ -277,6 +277,14 @@ public function getViewsData() {
+    $data['users']['user_bulk_form'] = array(
+      'title' => t('Bulk update'),
+      'help' => t('Add a form element that lets you run operations on multiple users.'),
+      'field' => array(
+        'id' => 'user_bulk_form',
+      ),
+    );

this is a bit problematic now ... given that views_views_data() might run afterwards. If we move it out of the alter hook, maybe the bulk form part of views_views_data() should move into an alter hook?

dawehner’s picture

Status: Reviewed & tested by the community » Needs work

.

pcambra’s picture

I was reviewing this and this is what views module does on behalf of any module that defines an entity and has actions associated with it (in views.views.inc):

    $data[$entity_info->getBaseTable()][$entity_type . '_bulk_form'] = array(
      'title' => t('Bulk update'),
      'help' => t('Allows users to apply an action to one or more items.'),
      'field' => array(
        'id' => 'bulk_form',
      ),
    );

Couldn't we just remove the particular node/user/* integrations then try to override that without adding much, honestly (in NodeViewsData.php):

$data['node']['node_bulk_form'] = array(
      'title' => t('Node operations bulk form'),
      'help' => t('Add a form element that lets you run operations on multiple nodes.'),
      'field' => array(
        'id' => 'node_bulk_form',
      ),
);

Couldn't we just get rid of the particular implementations and leave just the generic view implementation, maybe with one change, adding the entity_type to the id of the field: 'id' => 'bulk_form', would be 'id' => $entity_type . '_bulk_form',

dawehner’s picture

Couldn't we just get rid of the particular implementations and leave just the generic view implementation, maybe with one change, adding the entity_type to the id of the field: 'id' => 'bulk_form', would be 'id' => $entity_type . '_bulk_form',

Note: This is the used plugin ID, not some arbitrary ID.

We adapt the behaviour in those subsclasses a bit already.

pcambra’s picture

Status: Needs work » Needs review

Ok, got it @dawehner, thanks.

Back to the original issue, tracing this comment, if I've understood it correctly:

this is a bit problematic now ... given that views_views_data() might run afterwards

It is actually views_views_data who invokes the *ViewsData.php plugins:

  // Registers views data for the entity itself.
  foreach (\Drupal::entityManager()->getDefinitions() as $entity_type_id => $entity_type) {
    if ($entity_type->hasHandlerClass('views_data')) {
      /** @var \Drupal\views\EntityViewsDataInterface $views_data */
      $views_data = \Drupal::entityManager()->getHandler($entity_type_id, 'views_data');
      $data = NestedArray::mergeDeep($data, $views_data->getViewsData());
    }
  }

So whatever we've got in views_view_data will come before the $views_data->getViewsData() call to the plugin.

If you add a test with something module_set_weight('node', 1000);, the order of execution is respected, views will always run first, I honestly don't see any value on adding this to the patch, setting back to needs review.

dawehner’s picture

Status: Needs review » Reviewed & tested by the community

If you add a test with something module_set_weight('node', 1000);, the order of execution is respected, views will always run first, I honestly don't see any value on adding this to the patch, setting back to needs review.

Total fair argumentation. Thank you!

alexpott’s picture

Status: Reviewed & tested by the community » Fixed

Nice clean up. Moving this out of the alter hook makes perfect sense and has no disruption.

Committed 3ae4f11 and pushed to 8.0.x. Thanks!

  • alexpott committed 3ae4f11 on 8.0.x
    Issue #2400153 by pcambra: Move bulk form data definition to their...

Status: Fixed » Closed (fixed)

Automatically closed - issue fixed for 2 weeks with no activity.