routeMatch = $route_match; } /** * {@inheritdoc} */ public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) { return new static( $configuration, $plugin_id, $plugin_definition, $container->get('current_route_match') ); } /** * {@inheritdoc} */ public function defaultConfiguration() { return [ 'content' => $this->t('Hello world!'), ]; } /** * {@inheritdoc} */ public function blockForm($form, FormStateInterface $form_state) { $form['content'] = [ '#type' => 'textarea', '#title' => $this->t('Block content'), '#default_value' => $this->configuration['content'], ]; return $form; } /** * {@inheritdoc} */ public function blockSubmit($form, FormStateInterface $form_state) { $this->configuration['content'] = $form_state->getValue('content'); } /** * {@inheritdoc} * * @DCG Remove this method of you do not need any access restrictions. */ protected function blockAccess(AccountInterface $account) { $route_name = $this->routeMatch->getRouteName(); // Display the block only for anonymous users. if ($account->isAnonymous() && $route_name != 'user.register') { return AccessResult::allowed() ->addCacheContexts(['route.name', 'user.roles:anonymous']); } return AccessResult::forbidden(); } /** * {@inheritdoc} */ public function build() { $build['content'] = [ '#markup' => $this->configuration['content'], ]; return $build; } }