0

I have created a custom column in the content listing section using hook_views_data(). I want to add a button to the every row of that custom column. Can anyone help me with this issue?

function sortd_views_data() {
  $data['node_field_data']['SortdAction'] = [
    'title' => t('Sortd Action'),
    'help' => t('Manual Sync/Unsync articles.'),
    'field' => [
      'handler' => 'views_handler_field',
      'id' => 'numeric',
    ],
    'sort' => [
      'handler' => 'views_handler_sort',
      'click sortable' => TRUE,
    ],
    'filter' => [
      'handler' => 'views_handler_filter_string',
    ],
    'argument' => [
      'handler' => 'views_handler_argument_string',
    ],
  ];
  
  return $data;  
}

The 0 show for every row needs to be replaced with the button.

screenshot

I tried to solve the problem using the following code.

function sortd_preprocess_views_view_table(&$variables) {
  drupal_set_message($variables['rows']);
  if ($variables['view']->id() == 'content') {
    foreach ($variables['rows'] as &$row) {
      $row['SortdAction'] = [
        '#type' => 'submit',
        '#value' => t('Sortd'),
      ];
    }
  }
}

Whenever I try to print $variables['row'], it shows me the correct data in that table.

1 Answer 1

1
  1. In /admin/structure/views/view/YOUR_VIEW_ID, add a new field (Custom text)

  2. In your hook, use the following code

    function sortd_preprocess_views_view_field(&$vars){
      if ($vars['view']->id() == 'YOUR_VIEW_ID') {
        if ($vars['field']->field == 'nothing') {
          $vars['output'] = [
            '#markup' => "<div class='dropbutton-wrapper dropbutton-multiple'><div><ul class='dropbutton'><li class='edit dropbutton-action'></li></ul></div></div>",
          ]
        }
      }  
    }
    
  3. Clear the cache

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.