Im trying to display the last accessed field when a user logs in. For this I tried using User::getLastLoginTime() . But this returns a fatal error saying

PHP Fatal error: Call to undefined method Drupal\customize_block\Plugin\Block\ContractDetails::get() in /var/www/html/core/modules/user/src/Entity/User.php on line 265

Here is my block ,in my custom module:

 * Provides a 'Customized contract' Block
 * @Block(
 *   id = "customer_info",
 *   label = @Translation("Customer and contract info"),
 *   module = "user",
 *   context = {
 *     "current_user" = @ContextDefinition("entity:user", label = @Translation("Current User"))
 *   }
 * )
namespace Drupal\customize_block\Plugin\Block;

use Drupal\Core\Block\BlockBase;

class ContractInfo extends BlockBase {
 public function build() {
 global $base_url;
 $current_user = $this->getContext('current_user');
 $output.='<div>' . $current_user -> contextData . '</div>';

     return array(
      '#markup' => $output,
      '#cache' => array(
     'contexts' => array('url'),

Am I calling this method correct way?

1 Answer 1


getLastLoginTime is not a static method so you would not be able to call it statically like that. You need to get the current user object from a couple of different ways:

1. Use the Drupal::currentUser.

This is easy, but not recommended to tightly-couple your block.

2. Inject the current_user service into your Block by implementing ContainerFactoryPluginInterface.

The disadvantage is that this also has a dependency on the container, but can be mocked.

3. Use the Context system because blocks extend ContextAwarePluginBase.

This is the recommended option in my opinion because there is already a current user context (as well as node contexts) for blocks.

 * @Block(
 *   ...
 *   context = {
 *     "current_user" = @ContextDefinition("type" = "entity:user", "label" => @Translation("Current User")
 *   }
 * )

Then $current_user = $this->getContext('current_user') will get the current user.

Unfortunately there is not a handbook page about using contexts so documentation is relegated to guessing things based on the change record, which itself was no longer accurate at the time of writing this answer.

Interestingly some IDEs (cough PhpStorm 10 cough) choke on the above annotation syntax because.


You should look at the documentation for render arrays and the changes to make markup safe.

  • I think this needs work if anyone wants to clarify and improve using ContextAware.
    – mradcliffe
    Mar 2, 2016 at 15:03
  • Thanks for the reply. Sorry, Im not aware of creating context for blocks. Should I attach this context when Im returning the block?
    – wilNev
    Mar 2, 2016 at 15:35
  • 1
    You should add the context hash above to your block annotation.
    – mradcliffe
    Mar 2, 2016 at 16:10
  • Ive added the context hash above the block. Im getting another fatal error this time : PHP Fatal error: Call to a member function isRequired() on a non-object in /var/www/html/core/lib/Drupal/Core/Plugin/Context/ContextHandler.php on line 105 . Please look the updated code above. Is there anything that Im missing ?
    – wilNev
    Mar 2, 2016 at 19:53
  • I tried giving '@Plugin' instead of '@Block' , this time the page gets loaded but the block isnt showing up. It says " This block is broken or missing. You may be missing content or you might need to enable the original module." in the html.
    – wilNev
    Mar 2, 2016 at 20:45

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.