class SettingsForm

Same name in other branches
  1. 4.x src/Form/SettingsForm.php \Drupal\devel\Form\SettingsForm

Defines a form that configures devel settings.

Hierarchy

Expanded class hierarchy of SettingsForm

1 string reference to 'SettingsForm'
devel.routing.yml in ./devel.routing.yml
devel.routing.yml

File

src/Form/SettingsForm.php, line 17

Namespace

Drupal\devel\Form
View source
class SettingsForm extends ConfigFormBase {
    protected DevelDumperPluginManagerInterface $dumperManager;
    
    /**
     * The 'devel.settings' config object.
     */
    protected Config $config;
    
    /**
     * {@inheritdoc}
     */
    public static function create(ContainerInterface $container) : static {
        $instance = parent::create($container);
        $instance->dumperManager = $container->get('plugin.manager.devel_dumper');
        $instance->config = $container->get('config.factory')
            ->getEditable('devel.settings');
        $instance->stringTranslation = $container->get('string_translation');
        return $instance;
    }
    
    /**
     * {@inheritdoc}
     */
    public function getFormId() : string {
        return 'devel_admin_settings_form';
    }
    
    /**
     * {@inheritdoc}
     */
    protected function getEditableConfigNames() : array {
        return [
            'devel.settings',
        ];
    }
    
    /**
     * {@inheritdoc}
     */
    public function buildForm(array $form, FormStateInterface $form_state, ?Request $request = NULL) : array {
        $current_url = Url::createFromRequest($request);
        $form['page_alter'] = [
            '#type' => 'checkbox',
            '#title' => $this->t('Display $attachments array'),
            '#default_value' => $this->config
                ->get('page_alter'),
            '#description' => $this->t('Display $attachments array from <a href="https://api.drupal.org/api/drupal/core%21lib%21Drupal%21Core%21Render%21theme.api.php/function/hook_page_attachments_alter/10">hook_page_attachments_alter()</a> in the messages area of each page.'),
        ];
        $form['raw_names'] = [
            '#type' => 'checkbox',
            '#title' => $this->t('Display machine names of permissions and modules'),
            '#default_value' => $this->config
                ->get('raw_names'),
            '#description' => $this->t('Display the language-independent machine names of the permissions in mouse-over hints on the <a href=":permissions_url">Permissions</a> page and the module base file names on the Permissions and <a href=":modules_url">Modules</a> pages.', [
                ':permissions_url' => Url::fromRoute('user.admin_permissions')->toString(),
                ':modules_url' => Url::fromRoute('system.modules_list')->toString(),
            ]),
        ];
        $form['rebuild_theme'] = [
            '#type' => 'checkbox',
            '#title' => $this->t('Rebuild the theme registry on every page load'),
            '#description' => $this->t('New templates, theme overrides, and changes to the theme.info.yml need the theme registry to be rebuilt in order to appear on the site.'),
            '#default_value' => $this->config
                ->get('rebuild_theme'),
        ];
        $error_handlers = devel_get_handlers();
        $form['error_handlers'] = [
            '#type' => 'select',
            '#title' => $this->t('Error handlers'),
            '#options' => [
                DEVEL_ERROR_HANDLER_NONE => $this->t('None'),
                DEVEL_ERROR_HANDLER_STANDARD => $this->t('Standard Drupal'),
                DEVEL_ERROR_HANDLER_BACKTRACE_DPM => $this->t('Backtrace in the message area'),
                DEVEL_ERROR_HANDLER_BACKTRACE_KINT => $this->t('Backtrace above the rendered page'),
            ],
            '#multiple' => TRUE,
            '#default_value' => empty($error_handlers) ? DEVEL_ERROR_HANDLER_NONE : $error_handlers,
            '#description' => [
                [
                    '#markup' => $this->t('Select the error handler(s) to use, in case you <a href=":choose">choose to show errors on screen</a>.', [
                        ':choose' => Url::fromRoute('system.logging_settings')->toString(),
                    ]),
                ],
                [
                    '#theme' => 'item_list',
                    '#items' => [
                        $this->t('<em>None</em> is a good option when stepping through the site in your debugger.'),
                        $this->t('<em>Standard Drupal</em> does not display all the information that is often needed to resolve an issue.'),
                        $this->t('<em>Backtrace</em> displays nice debug information when any type of error is noticed, but only to users with the %perm permission.', [
                            '%perm' => $this->t('Access developer information'),
                        ]),
                    ],
                ],
                [
                    '#markup' => $this->t('Depending on the situation, the theme, the size of the call stack and the arguments, etc., some handlers may not display their messages, or display them on the subsequent page. Select <em>Standard Drupal</em> <strong>and</strong> <em>Backtrace above the rendered page</em> to maximize your chances of not missing any messages.') . '<br />' . $this->t('Demonstrate the current error handler(s):') . ' ' . Link::fromTextAndUrl('notice', $current_url->setOption('query', [
                        'demo' => 'notice',
                    ]))
                        ->toString() . ', ' . Link::fromTextAndUrl('notice+warning', $current_url->setOption('query', [
                        'demo' => 'warning',
                    ]))
                        ->toString() . ', ' . Link::fromTextAndUrl('notice+warning+error', $current_url->setOption('query', [
                        'demo' => 'error',
                    ]))
                        ->toString() . ' (' . $this->t('The presentation of the @error is determined by PHP.', [
                        '@error' => 'error',
                    ]) . ')',
                ],
            ],
        ];
        $form['error_handlers']['#size'] = count($form['error_handlers']['#options']);
        if ($request->query
            ->has('demo')) {
            if ($request->getMethod() === 'GET') {
                $this->demonstrateErrorHandlers($request->query
                    ->get('demo'));
            }
            $request->query
                ->remove('demo');
        }
        $dumper = $this->config
            ->get('devel_dumper');
        $default = $this->dumperManager
            ->isPluginSupported($dumper) ? $dumper : $this->dumperManager
            ->getFallbackPluginId('');
        $form['dumper'] = [
            '#type' => 'radios',
            '#title' => $this->t('Variables Dumper'),
            '#options' => [],
            '#default_value' => $default,
            '#description' => $this->t('Select the debugging tool used for formatting and displaying the variables inspected through the debug functions of Devel. <strong>NOTE</strong>: Some of these plugins require external libraries for to be enabled. Learn how install external libraries with <a href=":url">Composer</a>.', [
                ':url' => 'https://www.drupal.org/node/2404989',
            ]),
        ];
        foreach ($this->dumperManager
            ->getDefinitions() as $id => $definition) {
            $form['dumper']['#options'][$id] = $definition['label'];
            $supported = $this->dumperManager
                ->isPluginSupported($id);
            $form['dumper'][$id]['#disabled'] = !$supported;
            $form['dumper'][$id]['#description'] = [
                '#type' => 'inline_template',
                '#template' => '{{ description }}{% if not supported %}<div><small>{% trans %}<strong>Not available</strong>. You may need to install external dependencies for use this plugin.{% endtrans %}</small></div>{% endif %}',
                '#context' => [
                    'description' => $definition['description'],
                    'supported' => $supported,
                ],
            ];
        }
        // Allow custom debug filename for use in DevelDumperManager::debug()
        $default_file = $this->config
            ->get('debug_logfile') ?: 'temporary://drupal_debug.txt';
        $form['debug_logfile'] = [
            '#type' => 'textfield',
            '#title' => $this->t('Debug Log File'),
            '#description' => $this->t('This is the log file that Devel functions such as ddm() write to. Use temporary:// to represent your systems temporary directory. Save with a blank filename to revert to the default.'),
            '#default_value' => $default_file,
        ];
        return parent::buildForm($form, $form_state);
    }
    
    /**
     * {@inheritdoc}
     */
    public function submitForm(array &$form, FormStateInterface $form_state) : void {
        $values = $form_state->getValues();
        $this->config
            ->set('page_alter', $values['page_alter'])
            ->set('raw_names', $values['raw_names'])
            ->set('error_handlers', $values['error_handlers'])
            ->set('rebuild_theme', $values['rebuild_theme'])
            ->set('devel_dumper', $values['dumper'])
            ->set('debug_logfile', $values['debug_logfile'] ?: 'temporary://drupal_debug.txt')
            ->save();
        parent::submitForm($form, $form_state);
    }
    
    /**
     * Demonstrates the capabilities of the error handler.
     *
     * @param string $severity
     *   The severity level for which demonstrate the error handler capabilities.
     */
    protected function demonstrateErrorHandlers(string $severity) : void {
        switch ($severity) {
            case 'notice':
                trigger_error('This is an example notice', E_USER_NOTICE);
                break;
            case 'warning':
                trigger_error('This is an example notice', E_USER_NOTICE);
                trigger_error('This is an example warning', E_USER_WARNING);
                break;
            case 'error':
                trigger_error('This is an example notice', E_USER_NOTICE);
                trigger_error('This is an example warning', E_USER_WARNING);
                trigger_error('This is an example error', E_USER_ERROR);
        }
    }

}

Members

Title Sort descending Modifiers Object type Summary Overriden Title Overrides
ConfigFormBase::CONFIG_KEY_TO_FORM_ELEMENT_MAP protected constant The $form_state key which stores a map of config keys to form elements.
ConfigFormBase::copyFormValuesToConfig private static function Copies form values to Config keys.
ConfigFormBase::doStoreConfigMap protected function Helper method for #after_build callback ::storeConfigKeyToFormElementMap().
ConfigFormBase::formatMultipleViolationsMessage protected function Formats multiple violation messages associated with a single form element. 1
ConfigFormBase::loadDefaultValuesFromConfig public function Process callback to recursively load default values from #config_target.
ConfigFormBase::storeConfigKeyToFormElementMap public function #after_build callback which stores a map of element names to config keys.
ConfigFormBase::typedConfigManager protected function Returns the typed config manager service.
ConfigFormBase::validateForm public function Form validation handler. Overrides FormBase::validateForm 13
ConfigFormBase::__construct public function Constructs a \Drupal\system\ConfigFormBase object. 17
ConfigFormBaseTrait::config protected function Retrieves a configuration object.
DependencySerializationTrait::$_entityStorages protected property
DependencySerializationTrait::$_serviceIds protected property
DependencySerializationTrait::__sleep public function 1
DependencySerializationTrait::__wakeup public function 2
FormBase::$configFactory protected property The config factory. 3
FormBase::$requestStack protected property The request stack. 1
FormBase::$routeMatch protected property The route match.
FormBase::configFactory protected function Gets the config factory for this form. 3
FormBase::container private function Returns the service container.
FormBase::currentUser protected function Gets the current user. 2
FormBase::getRequest protected function Gets the request object.
FormBase::getRouteMatch protected function Gets the route match.
FormBase::logger protected function Gets the logger for a specific channel.
FormBase::redirect protected function Returns a redirect response object for the specified route.
FormBase::resetConfigFactory public function Resets the configuration factory.
FormBase::setConfigFactory public function Sets the config factory for this form.
FormBase::setRequestStack public function Sets the request stack object to use.
LoggerChannelTrait::$loggerFactory protected property The logger channel factory service.
LoggerChannelTrait::getLogger protected function Gets the logger for a specific channel.
LoggerChannelTrait::setLoggerFactory public function Injects the logger channel factory.
MessengerTrait::$messenger protected property The messenger. 16
MessengerTrait::messenger public function Gets the messenger. 16
MessengerTrait::setMessenger public function Sets the messenger.
RedirectDestinationTrait::$redirectDestination protected property The redirect destination service. 2
RedirectDestinationTrait::getDestinationArray protected function Prepares a &#039;destination&#039; URL query parameter for use with \Drupal\Core\Url.
RedirectDestinationTrait::getRedirectDestination protected function Returns the redirect destination service.
RedirectDestinationTrait::setRedirectDestination public function Sets the redirect destination service.
SettingsForm::$config protected property The &#039;devel.settings&#039; config object.
SettingsForm::$dumperManager protected property
SettingsForm::buildForm public function Form constructor. Overrides ConfigFormBase::buildForm
SettingsForm::create public static function Instantiates a new instance of this class. Overrides ConfigFormBase::create
SettingsForm::demonstrateErrorHandlers protected function Demonstrates the capabilities of the error handler.
SettingsForm::getEditableConfigNames protected function Gets the configuration names that will be editable. Overrides ConfigFormBaseTrait::getEditableConfigNames
SettingsForm::getFormId public function Returns a unique string identifying the form. Overrides FormInterface::getFormId
SettingsForm::submitForm public function Form submission handler. Overrides ConfigFormBase::submitForm
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.