Score:-1

How do I add link to Edit and Delete in a column in a view table item list?

gp flag

I've trying to add a link to Edit and Delete column to perform desirable actions. I need help in implementing it. I've tried several suggested methods on the internet, but none did the job for me. Maybe I am doing something wrong.

Here is my Controller.php

public function getAdList(){
            $limit = 3;
            $query = \Drupal::database();
            $result = $query->select('ad_form_submissions','u')
                    ->fields('u',['id','name','crop_category','crop_type','quantity','price'])
                    ->extend('Drupal\Core\Database\Query\PagerSelectExtender')->limit($limit)
                    ->execute()->fetchAll(\PDO::FETCH_OBJ);

            $data = [];
            $count = 1;

            $params = \Drupal::request()->query->all();

            if(empty($params) || $params['page'] == 0){
                $count=1;
            }else if($params['page'] == 1){
                $count = $params['page'] + $limit;
            }else{
                $count = $params['page'] * limit;
                $count++;
            }

            foreach($result as $row){
                $data[] = [
                    'S_no'=> $count.".",
                    'ID'=> $row->id,
                    'Name'=> $row->name,
                    'Crop Category'=> $row->crop_category,
                    'Crop Type'=> $row->crop_type,
                    'Quantity'=> $row->quantity,
                    'Price'=> $row->price,
                    'Edit' => 'Edit',
                    'Delete'=>'Delete'
                ];
                $count++;
            }

            $header = array('S_no','ID','Name','Crop Category','Crop Type','Quantity','Price','Edit','Delete');

            $build['table'] = [
                '#type'=>'table',
                '#header'=>$header,
                '#rows'=>$data,
                '#empty' => t('No users found'),
            ];

            $build['parger'] = [
                '#type'=>'pager'
            ];

            return [
                $build,
                '#title'=> 'Submit Ad List'
            ];
        
        }

Clarification: I want to perform the Edit and Delete Task on the selected row.

Score:0
cn flag

I believe you are looking for Entity Operations. Soul you'd have to load the enity by ID, and then use the getoperations methods. Looking at your code though, I would suggest you use EntityTypeManager to instead load your entities with a EntityListController or something equivalent, it's all baked into core.

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.