1

I'm a first-time stackexchange user, long time Drupal 6 developer.

I'm just starting playing with Drupal 8 and I know I'm going to love it when I get used to working in it but right now I'm in that frustration phase where it's unfamiliar again and I can't do even the simple things! I wonder if you can help.

I would like to add dynamic content (probably just #markup) to the user's main tab. At the moment, the user tab only contains the familiar text 'Member for xxx'.

Under Drupal 6 I just handled hook_user, something like:

function mymodule_user($type, &$edit, &$user, $category = NULL) {
  switch ($type) {
    case 'view':
      $user->content['the_time'] = array(
        '#type' => 'markup',
        '#value' => 'Epoch = <b>' . time() . '</b>',
        '#weight' => 100,
      );          
      break;
  }  
}

(I've not checked the above actually runs, but it's a modified version of something I'm using on a live site).

What's the equivalent under Drupal 8? I'm looking at the following:

function mymodule_user_view(array &$build, UserInterface $account, EntityViewDisplayInterface $display) {
  $build['the_time'] = array(
    '#type' => 'item',
    '#markup' => 'Epoch = <b>' . time() . '</b>',
  );
}

Although this works (after clearing the caches) I don't think this is the recommended way to do it, as I'm not seeing dynamic content, the hook is only called once and the text is cached.

I really hope someone can point me in the right direction!

1 Answer 1

3

The question how to put a custom field in the user tab is already answered, btw you don't need '#type', when using '#markup' in drupal 8.

As you said at the end, this is a caching issue. In general it is not a good idea to put the time in the html of every page, you better do this in javascript. If you don't care about performance, then you can disable the cache:

$build['#cache']['max-age'] = 0;

This does not work for anonymous users, because in drupal 8 there is also the page cache enabled by default. If you deliver a different page on every request, you don't need this module and you can uninstall it.

If you want to disable it only for the user default tab, then use this command:

\Drupal::service('page_cache_kill_switch')->trigger();
2
  • Fantastic, thank you so much. I'm finding out all about caching in 8 now :) Commented Jan 30, 2016 at 9:07
  • Great answer, I'd like to add that should try to avoid having to disable the cache. in your specific example, you could do it in Javascript, possibly in combination with some static data that you e.g. return as data attributes or so. Or by adding cache contexts/tags to have it varied/invalidated by something. drupal.org/developing/api/8/render/arrays/cacheability has pretty good docs.
    – Berdir
    Commented Jan 30, 2016 at 14:02

Your Answer

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

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