class SwitchUserBlock

Same name in other branches
  1. 8.x-1.x src/Plugin/Block/SwitchUserBlock.php \Drupal\devel\Plugin\Block\SwitchUserBlock
  2. 4.x src/Plugin/Block/SwitchUserBlock.php \Drupal\devel\Plugin\Block\SwitchUserBlock

Provides a block for switching users.

Plugin annotation


@Block(
  id = "devel_switch_user",
  admin_label = @Translation("Switch user"),
  category = "Devel"
)

Hierarchy

Expanded class hierarchy of SwitchUserBlock

File

src/Plugin/Block/SwitchUserBlock.php, line 25

Namespace

Drupal\devel\Plugin\Block
View source
class SwitchUserBlock extends BlockBase implements ContainerFactoryPluginInterface {
    
    /**
     * The FormBuilder object.
     */
    protected FormBuilderInterface $formBuilder;
    
    /**
     * A helper for creating the user list form.
     */
    protected SwitchUserListHelper $switchUserListHelper;
    // phpcs:ignore Generic.CodeAnalysis.UselessOverridingMethod.Found
    public final function __construct(array $configuration, string $plugin_id, array $plugin_definition) {
        parent::__construct($configuration, $plugin_id, $plugin_definition);
    }
    
    /**
     * {@inheritdoc}
     */
    public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) : static {
        $instance = new static($configuration, $plugin_id, $plugin_definition);
        $instance->formBuilder = $container->get('form_builder');
        $instance->switchUserListHelper = $container->get('devel.switch_user_list_helper');
        $instance->stringTranslation = $container->get('string_translation');
        return $instance;
    }
    
    /**
     * {@inheritdoc}
     */
    public function defaultConfiguration() : array {
        return [
            'list_size' => 12,
            'include_anon' => FALSE,
            'show_form' => TRUE,
        ];
    }
    
    /**
     * {@inheritdoc}
     */
    protected function blockAccess(AccountInterface $account) {
        return AccessResult::allowedIfHasPermission($account, 'switch users');
    }
    
    /**
     * {@inheritdoc}
     */
    public function blockForm($form, FormStateInterface $form_state) {
        $anonymous = new AnonymousUserSession();
        $form['list_size'] = [
            '#type' => 'number',
            '#title' => $this->t('Number of users to display in the list'),
            '#default_value' => $this->configuration['list_size'],
            '#min' => 1,
            '#max' => 50,
        ];
        $form['include_anon'] = [
            '#type' => 'checkbox',
            '#title' => $this->t('Include %anonymous', [
                '%anonymous' => $anonymous->getDisplayName(),
            ]),
            '#default_value' => $this->configuration['include_anon'],
        ];
        $form['show_form'] = [
            '#type' => 'checkbox',
            '#title' => $this->t('Allow entering any user name'),
            '#default_value' => $this->configuration['show_form'],
        ];
        return $form;
    }
    
    /**
     * {@inheritdoc}
     */
    public function blockSubmit($form, FormStateInterface $form_state) : void {
        $this->configuration['list_size'] = $form_state->getValue('list_size');
        $this->configuration['include_anon'] = $form_state->getValue('include_anon');
        $this->configuration['show_form'] = $form_state->getValue('show_form');
    }
    
    /**
     * {@inheritdoc}
     */
    public function getCacheMaxAge() : int {
        return 0;
    }
    
    /**
     * {@inheritdoc}
     */
    public function build() : array {
        $build = [];
        if ($accounts = $this->switchUserListHelper
            ->getUsers($this->configuration['list_size'], $this->configuration['include_anon'])) {
            $build['devel_links'] = $this->switchUserListHelper
                ->buildUserList($accounts);
            if ($this->configuration['show_form']) {
                $build['devel_form'] = $this->formBuilder
                    ->getForm(SwitchUserForm::class);
            }
        }
        return $build;
    }

}

Members

Title Sort descending Modifiers Object type Summary Member alias Overriden Title Overrides
BlockBase::buildConfigurationForm public function Form constructor. Overrides PluginFormInterface::buildConfigurationForm 2
BlockPluginInterface::BLOCK_LABEL_VISIBLE constant Indicates the block label (title) should be displayed to end users.
BlockPluginTrait::$inPreview protected property Whether the plugin is being rendered in preview mode.
BlockPluginTrait::$transliteration protected property The transliteration service.
BlockPluginTrait::access public function
BlockPluginTrait::baseConfigurationDefaults protected function Returns generic default configuration for block plugins.
BlockPluginTrait::blockValidate public function 3
BlockPluginTrait::buildConfigurationForm public function Creates a generic configuration form for all block types. Individual
block plugins can add elements to this form by overriding
BlockBase::blockForm(). Most block plugins should not override this
method unless they need to alter the generic form elements.
Aliased as: traitBuildConfigurationForm
BlockPluginTrait::calculateDependencies public function 1
BlockPluginTrait::getConfiguration public function 1
BlockPluginTrait::getMachineNameSuggestion public function 1
BlockPluginTrait::getPreviewFallbackString public function 3
BlockPluginTrait::label public function
BlockPluginTrait::setConfiguration public function
BlockPluginTrait::setConfigurationValue public function
BlockPluginTrait::setInPreview public function
BlockPluginTrait::setTransliteration public function Sets the transliteration service.
BlockPluginTrait::submitConfigurationForm public function Most block plugins should not override this method. To add submission
handling for a specific block type, override BlockBase::blockSubmit().
BlockPluginTrait::transliteration protected function Wraps the transliteration service.
BlockPluginTrait::validateConfigurationForm public function Most block plugins should not override this method. To add validation
for a specific block type, override BlockBase::blockValidate().
1
ContextAwarePluginAssignmentTrait::addContextAssignmentElement protected function Builds a form element for assigning a context to a given slot.
ContextAwarePluginAssignmentTrait::contextHandler protected function Wraps the context handler.
ContextAwarePluginTrait::$context protected property The data objects representing the context of this plugin.
ContextAwarePluginTrait::getCacheContexts public function 10
ContextAwarePluginTrait::getCacheTags public function 3
ContextAwarePluginTrait::getContext public function
ContextAwarePluginTrait::getContextDefinition public function
ContextAwarePluginTrait::getContextDefinitions public function
ContextAwarePluginTrait::getContextMapping public function 1
ContextAwarePluginTrait::getContexts public function
ContextAwarePluginTrait::getContextValue public function
ContextAwarePluginTrait::getContextValues public function
ContextAwarePluginTrait::getPluginDefinition abstract public function 1
ContextAwarePluginTrait::setContext public function 1
ContextAwarePluginTrait::setContextMapping public function
ContextAwarePluginTrait::setContextValue public function
ContextAwarePluginTrait::validateContexts public function
DerivativeInspectionInterface::getBaseId public function Gets the base_plugin_id of the plugin instance. 1
DerivativeInspectionInterface::getDerivativeId public function Gets the derivative_id of the plugin instance. 1
MessengerTrait::$messenger protected property The messenger. 16
MessengerTrait::messenger public function Gets the messenger. 16
MessengerTrait::setMessenger public function Sets the messenger.
PluginInspectionInterface::getPluginId public function Gets the plugin ID of the plugin instance. 2
PluginWithFormsTrait::getFormClass public function Implements \Drupal\Core\Plugin\PluginWithFormsInterface::getFormClass().
PluginWithFormsTrait::hasFormClass public function Implements \Drupal\Core\Plugin\PluginWithFormsInterface::hasFormClass().
StringTranslationTrait::$stringTranslation protected property The string translation service. 3
StringTranslationTrait::formatPlural protected function Formats a string containing a count of items.
StringTranslationTrait::getNumberOfPlurals protected function Returns the number of plurals supported by a given language.
StringTranslationTrait::getStringTranslation protected function Gets the string translation service.
StringTranslationTrait::setStringTranslation public function Sets the string translation service to use. 2
StringTranslationTrait::t protected function Translates a string to the current language or to a given language.
SwitchUserBlock::$formBuilder protected property The FormBuilder object.
SwitchUserBlock::$switchUserListHelper protected property A helper for creating the user list form.
SwitchUserBlock::blockAccess protected function Indicates whether the block should be shown. Overrides BlockPluginTrait::blockAccess
SwitchUserBlock::blockForm public function Overrides BlockPluginTrait::blockForm
SwitchUserBlock::blockSubmit public function Overrides BlockPluginTrait::blockSubmit
SwitchUserBlock::build public function Builds and returns the renderable array for this block plugin. Overrides BlockPluginInterface::build
SwitchUserBlock::create public static function Creates an instance of the plugin. Overrides ContainerFactoryPluginInterface::create
SwitchUserBlock::defaultConfiguration public function Overrides BlockPluginTrait::defaultConfiguration
SwitchUserBlock::getCacheMaxAge public function Overrides ContextAwarePluginTrait::getCacheMaxAge
SwitchUserBlock::__construct final public function Overrides BlockPluginTrait::__construct