Score:0

How can I add a button to every row of custom column?

np flag

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.

Score:1
tg flag
  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

I sit in a Tesla and translated this thread with Ai:

mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.