Here, We are going to define a custom field for the view to output our actions dropdown.
- Define routes for actions, we can take an example from drupal documentation.
Use
@ViewsField
annotation to define your field underPlugin\views\field
.<?php namespace Drupal\drupaljournal\Plugin\views\field; use Drupal\Core\Url; use Drupal\views\Plugin\views\field\FieldPluginBase; use Drupal\views\ResultRow; /** * A handler to provide a field of actions for a user reporting. * * @ingroup views_field_handlers * * @ViewsField("user_reporting_actions_views_field") */ class UserReportingActionsViewsField extends FieldPluginBase { /** * {@inheritdoc} */ public function usesGroupBy() { return FALSE; } /** * {@inheritdoc} */ public function query() { // Do nothing -- to override the parent query. } /** * {@inheritdoc} */ protected function defineOptions() { $options = parent::defineOptions(); $options['hide_alter_empty'] = ['default' => FALSE]; return $options; } /** * {@inheritdoc} */ public function render(ResultRow $values) { $entity = $this->getEntity($values); $operations['dismiss'] = [ 'title' => 'Dismiss report', 'url' => Url::fromRoute('drupaljournal_user.dismiss_report', ['user' => $entity->id()]), 'weight' => 0, ]; $operations['delete_image'] = [ 'title' => 'Delete image', 'url' => Url::fromRoute('drupaljournal_user.delete_image_report', ['user' => $entity->id()]), 'weight' => 1, ]; $operations['delete_bio'] = [ 'title' => 'Delete Bio', 'url' => Url::fromRoute('drupaljournal_user.delete_bio_report', ['user' => $entity->id()]), 'weight' => 2, ]; $operations['delete_bio_image'] = [ 'title' => 'Delete Bio+Image', 'url' => Url::fromRoute('drupaljournal_user.delete_bio_image_report', ['user' => $entity->id()]), 'weight' => 3, ]; $build = [ '#type' => 'operations', '#links' => $operations, ]; return $build; } }
- Create
mymodule.views.inc
file under the module directory. Use
hook_views_data_alter
to add your field.<?php /** * Implements hook_views_data_alter(). */ function drupaljournal_views_data_alter(array &$data) { $data['reporting']['table']['group'] = t('User'); $data['reporting']['table']['join'] = [ '#global' => [], ]; $data['reporting']['user_reporting_actions_views_field'] = [ 'title' => t('Actions'), 'field' => [ 'title' => t('Actions'), 'help' => t('Custom actions for a user reporting.'), 'id' => 'user_reporting_actions_views_field', ], ]; }
Output