0

In drupal 8, I am writing a custom module and in one of the function in controller -I am fetching data from an external API, then displaying it in table format using -

 foreach($rec as $data) {
    ksm($data);
    $rows[] = [// mydata
]};

return [
    '#type' => 'table',
    '#header' => $header,
    '#rows' => $rows,

  ];

This is working correctly.

But I want table cells to be editable and when user edits the data, a listener gets invoked (or provide an update button for the user). Now this listener would do some stuff with updated data(make external API call). My doubt is how can we do this with tables? Is there any way to make some columns of table editable and register a listener in Drupal.

Would I need to create a node for each row of this table data? If a node is required, then how to do ?

I am new to Drupal 8, just started a month ago, kindly help. Thanks in advance.

0

1 Answer 1

1

What you can do is first create nodes using the API data and create a table view. so that the views fields can be referenced to nodes and they can be updated. By this way you wont be needing any additional event handlers.

To Create Nodes you can use the following snippet:

use \Drupal\node\Entity\Node;

$node = Node::create([

       'type' => 'new_content_type',

       'title' => 'Creating a new node',

       'body' => 'Body of the node',

       'field_mail'=>'[email protected]',

       'field_link'=>'http://www.example.com',

       'field_date'=>[ '2017-07-22', ],

]);  
$node->enforceIsNew();
$node->save();

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.