class SettingsForm

Same name in this branch
  1. 11.x core/modules/media_library/src/Form/SettingsForm.php \Drupal\media_library\Form\SettingsForm
Same name and namespace in other branches
  1. 9 core/modules/media_library/src/Form/SettingsForm.php \Drupal\media_library\Form\SettingsForm
  2. 9 core/modules/aggregator/src/Form/SettingsForm.php \Drupal\aggregator\Form\SettingsForm
  3. 8.9.x core/modules/media_library/src/Form/SettingsForm.php \Drupal\media_library\Form\SettingsForm
  4. 8.9.x core/modules/aggregator/src/Form/SettingsForm.php \Drupal\aggregator\Form\SettingsForm
  5. 10 core/modules/media_library/src/Form/SettingsForm.php \Drupal\media_library\Form\SettingsForm

Configure Navigation settings for this site.

@internal

Hierarchy

Expanded class hierarchy of SettingsForm

1 string reference to 'SettingsForm'
navigation.routing.yml in core/modules/navigation/navigation.routing.yml
core/modules/navigation/navigation.routing.yml

File

core/modules/navigation/src/Form/SettingsForm.php, line 25

Namespace

Drupal\navigation\Form
View source
final class SettingsForm extends ConfigFormBase {
    
    /**
     * The file system service.
     *
     * @var \Drupal\Core\File\FileSystemInterface
     */
    protected $fileSystem;
    
    /**
     * The file usage service.
     *
     * @var \Drupal\file\FileUsage\FileUsageInterface
     */
    protected $fileUsage;
    
    /**
     * The file URL generator.
     *
     * @var \Drupal\Core\File\FileUrlGeneratorInterface
     */
    protected $fileUrlGenerator;
    
    /**
     * Renderer service.
     *
     * @var \Drupal\Core\Render\RendererInterface
     */
    protected RendererInterface $renderer;
    
    /**
     * Constructs a Navigation SettingsForm object.
     *
     * @param \Drupal\Core\Config\ConfigFactoryInterface $config_factory
     *   The factory for configuration objects.
     * @param \Drupal\Core\Config\TypedConfigManagerInterface $typed_config_manager
     *   The typed config manager.
     * @param \Drupal\Core\File\FileSystemInterface $file_system
     *   File system service.
     * @param \Drupal\Core\File\FileUrlGeneratorInterface $fileUrlGenerator
     *   The file URL generator.
     * @param \Drupal\file\FileUsage\FileUsageInterface $fileUsage
     *   The File Usage service.
     * @param \Drupal\Core\Render\RendererInterface $renderer
     *   Renderer service.
     */
    public function __construct(ConfigFactoryInterface $config_factory, TypedConfigManagerInterface $typed_config_manager, FileSystemInterface $file_system, FileUrlGeneratorInterface $fileUrlGenerator, FileUsageInterface $fileUsage, RendererInterface $renderer) {
        parent::__construct($config_factory, $typed_config_manager);
        $this->fileSystem = $file_system;
        $this->fileUrlGenerator = $fileUrlGenerator;
        $this->fileUsage = $fileUsage;
        $this->renderer = $renderer;
    }
    
    /**
     * {@inheritdoc}
     */
    public static function create(ContainerInterface $container) {
        return new static($container->get('config.factory'), $container->get('config.typed'), $container->get('file_system'), $container->get('file_url_generator'), $container->get('file.usage'), $container->get('renderer'));
    }
    
    /**
     * {@inheritdoc}
     */
    public function getFormId() : string {
        return 'navigation_settings';
    }
    
    /**
     * {@inheritdoc}
     */
    protected function getEditableConfigNames() : array {
        return [
            'navigation.settings',
        ];
    }
    
    /**
     * {@inheritdoc}
     */
    public function buildForm(array $form, FormStateInterface $form_state) : array {
        $config = $this->config('navigation.settings');
        $form['#attached']['library'][] = 'core/drupal.states';
        $form['logo'] = [
            '#type' => 'fieldset',
            '#title' => $this->t('Logo options'),
        ];
        $form['logo']['logo_provider'] = [
            '#type' => 'radios',
            '#title' => $this->t('Choose logo handling'),
            '#title_display' => 'invisible',
            '#options' => [
                NavigationRenderer::LOGO_PROVIDER_DEFAULT => $this->t('Default logo'),
                NavigationRenderer::LOGO_PROVIDER_HIDE => $this->t('Hide logo'),
                NavigationRenderer::LOGO_PROVIDER_CUSTOM => $this->t('Custom logo'),
            ],
            '#default_value' => $config->get('logo_provider'),
        ];
        $form['logo']['image'] = [
            '#type' => 'container',
            '#states' => [
                'visible' => [
                    ':input[name="logo_provider"]' => [
                        'value' => NavigationRenderer::LOGO_PROVIDER_CUSTOM,
                    ],
                ],
            ],
        ];
        $allowed = 'png jpg jpeg';
        $current_logo_managed_fid = $config->get('logo_managed');
        $max_navigation_allowed = $config->get('logo_max_filesize');
        $max_system_allowed = Environment::getUploadMaxSize();
        $max_allowed = $max_navigation_allowed < $max_system_allowed ? $max_navigation_allowed : $max_system_allowed;
        $upload_validators = [
            'FileExtension' => [
                'extensions' => $allowed,
            ],
            'FileSizeLimit' => [
                'fileLimit' => $max_allowed,
            ],
        ];
        $file_upload_help = [
            '#theme' => 'file_upload_help',
            '#description' => $this->t('Recommended image dimension 40 x 40 pixels.'),
            '#upload_validators' => $upload_validators,
            '#cardinality' => 1,
        ];
        $form['logo']['image']['logo_managed'] = [
            '#type' => 'managed_file',
            '#title' => t('Choose custom logo'),
            '#upload_validators' => $upload_validators,
            '#upload_location' => 'public://navigation-logo',
            '#description' => $this->renderer
                ->renderInIsolation($file_upload_help),
            '#default_value' => $current_logo_managed_fid,
            '#multiple' => FALSE,
        ];
        return parent::buildForm($form, $form_state);
    }
    
    /**
     * {@inheritdoc}
     */
    public function validateForm(array &$form, FormStateInterface $form_state) : void {
        $logo_managed = $form_state->getValue('logo_managed');
        if ($form_state->getValue('logo_provider') === NavigationRenderer::LOGO_PROVIDER_CUSTOM && empty($logo_managed) === TRUE) {
            $form_state->setErrorByName('logo_managed', 'An image file is required with the current logo handling option.');
        }
    }
    
    /**
     * {@inheritdoc}
     */
    public function submitForm(array &$form, FormStateInterface $form_state) : void {
        $config = $this->config('navigation.settings');
        // Get the previous config settings.
        $previous_logo_provider = $config->get('logo_provider');
        $logo_managed = $config->get('logo_managed');
        $previous_logo_fid = $logo_managed ? reset($logo_managed) : NULL;
        // Get new values from the form.
        $new_logo_provider = $form_state->getValue('logo_provider');
        $logo = $form_state->getValue('logo_managed');
        $new_logo_fid = !empty($logo) ? reset($logo) : NULL;
        // Pre-load files if any for FileUsageInterface.
        $previous_logo_managed = $previous_logo_fid ? File::load($previous_logo_fid) : NULL;
        $new_logo_managed = $new_logo_fid ? File::load($new_logo_fid) : NULL;
        // Decrement if previous logo_provider was 'custom' and has changed to a
        // different fid and there's a change in the logo fid.
        if ($previous_logo_provider === NavigationRenderer::LOGO_PROVIDER_CUSTOM && ($new_logo_provider !== NavigationRenderer::LOGO_PROVIDER_CUSTOM || $previous_logo_fid !== $new_logo_fid) && $previous_logo_managed) {
            $this->fileUsage
                ->delete($previous_logo_managed, 'navigation', 'logo', 1);
        }
        // Increment usage if different from the previous one.
        if ($new_logo_managed && $new_logo_fid !== $previous_logo_fid) {
            $new_logo_managed->setPermanent();
            $new_logo_managed->save();
            $this->fileUsage
                ->add($new_logo_managed, 'navigation', 'logo', 1);
        }
        $config->set('logo_provider', $form_state->getValue('logo_provider'))
            ->set('logo_managed', $form_state->getValue('logo_managed'))
            ->save();
        parent::submitForm($form, $form_state);
    }

}

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.
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. 2
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. 2
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::$fileSystem protected property The file system service.
SettingsForm::$fileUrlGenerator protected property The file URL generator.
SettingsForm::$fileUsage protected property The file usage service.
SettingsForm::$renderer protected property Renderer service.
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::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
SettingsForm::validateForm public function Form validation handler. Overrides ConfigFormBase::validateForm
SettingsForm::__construct public function Constructs a Navigation SettingsForm object. Overrides ConfigFormBase::__construct
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.

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.