0

I have created simple form and page to list records on form via Custom Drupal 8 Module. This form is accessible to only logged-in users with some specific role. It looks like screenshot. Whenever I submit new entry it lists record in via theme table for admin user but it does not appear for other logged-in users until I clear cache manually.

Please suggest, Thanks!

To display Records :

<?php

namespace Drupal\custom_form\Controller;
use Drupal\Core\Controller\ControllerBase;

class DisplayRecords extends ControllerBase {

  public function DisplayCustomRecords() {

    // Display theme table

    $table_header = [
      'name' => t('Name'),
      'mobilenumber' => t('Phone Number'),
      'email' => t('Email'),
      'age' => t('Age'),
      'gender' => t('Gender')
    ];

    $results = \Drupal::database()->select('custom_for_data')
      ->fields('custom_for_data', [
        'name',
        'mobilenumber',
        'email',
        'age',
        'gender'
      ])
      ->execute()
      ->fetchAll();

    $rows = array();
    foreach ($results as $res) {
      $formatted_age = \Drupal::service('date.formatter')->format(
        $res->age, 'custom', 'Y-m-d'
      );
      $rows[] = [
        'name' => $res->name,
        'mobilenumber' => $res->mobilenumber,
        'email' => $res->email,
        'age' => $formatted_age,
        'gender' => $res->gender
      ];
    }

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

    return $form;

  }


}

enter image description here

  • 1
    Please don’t add solutions to questions - that’s why we have answers :) – Clive Jan 27 '18 at 16:02
3

Short version: You need to define a cache tag that you can invalidate when a record is created, updated or deleted.

See https://www.drupal.org/docs/8/api/render-api/cacheability-of-render-arrays for a lot of documentation on this.

I'd also recommend to look into using an entity type for this instead of a custom table. A bit of effort at first, but you get a ton of functionality with that, including default cache tags and for example views integration, so you can build searchable, sortable listings like that through views.

  • I will look into details, thanks. As from your answers it sounds like recommended way is to create entity to store custom records rather than custom tables ? please suggest, that will be great if some more explanation will be added. – jas Jan 27 '18 at 10:49
  • sounds like recommended way is to create entity to store custom records rather than custom tables @jas Yes, that is what he is suggesting. – No Sssweat Jan 27 '18 at 10:55
  • @jas Better yet, you could build your form using the https://www.drupal.org/project/webform and then create a View to display the results of the form. – No Sssweat Jan 27 '18 at 11:01
  • 1
    Better depends on the use case. Webform is good at building a public form and then storing submissions that can be viewed by admins. I wouldn't use it for actual content that you want to manage. But yes, webform might be a good alternative for this use case as well but hard to say without knowing the whole use case :) – Berdir Jan 27 '18 at 12:17
  • Thanks, I was trying to learn how insert and select queries work with custom form so was working this way. By adding options:no_cache: TRUE its working now and records display without cache. Update added in my question. – jas Jan 27 '18 at 15:53
1

I was trying to learn how insert and select queries work with custom form so was using this way . By adding options:no_cache: TRUE its working now.

DisplayRecords:
    path: 'list/custom/records'
    defaults:
      '_title': 'Custom Records List'
      '_controller': 'Drupal\custom_form\Controller\DisplayRecords::DisplayCustomRecords'
    requirements:
       _permission: 'access_custom_records_display'
    options:
      no_cache: TRUE // with this line cache stops on page

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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