function SettingsForm::buildForm

Same name in this branch
  1. 11.x core/modules/media_library/src/Form/SettingsForm.php \Drupal\media_library\Form\SettingsForm::buildForm()
Same name in other branches
  1. 9 core/modules/media_library/src/Form/SettingsForm.php \Drupal\media_library\Form\SettingsForm::buildForm()
  2. 9 core/modules/aggregator/src/Form/SettingsForm.php \Drupal\aggregator\Form\SettingsForm::buildForm()
  3. 8.9.x core/modules/media_library/src/Form/SettingsForm.php \Drupal\media_library\Form\SettingsForm::buildForm()
  4. 8.9.x core/modules/aggregator/src/Form/SettingsForm.php \Drupal\aggregator\Form\SettingsForm::buildForm()
  5. 10 core/modules/media_library/src/Form/SettingsForm.php \Drupal\media_library\Form\SettingsForm::buildForm()
  6. 10 core/modules/navigation/src/Form/SettingsForm.php \Drupal\navigation\Form\SettingsForm::buildForm()

Overrides ConfigFormBase::buildForm

File

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

Class

SettingsForm
Configure Navigation settings for this site.

Namespace

Drupal\navigation\Form

Code

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'),
        ],
        '#config_target' => 'navigation.settings:logo.provider',
    ];
    $form['logo']['custom'] = [
        '#type' => 'container',
        '#states' => [
            'visible' => [
                ':input[name="logo_provider"]' => [
                    'value' => NavigationRenderer::LOGO_PROVIDER_CUSTOM,
                ],
            ],
        ],
    ];
    // If path is a public:// URI, display the path relative to the files
    // directory; stream wrappers are not end-user friendly.
    $original_path = $config->get('logo.path') ?? '';
    $friendly_path = NULL;
    $default_path = $original_path;
    $default = 'logo.png';
    if (StreamWrapperManager::getScheme($original_path) === 'public') {
        $friendly_path = StreamWrapperManager::getTarget($original_path);
        $default_path = $friendly_path;
    }
    // Prepare local file path for description.
    if ($original_path && isset($friendly_path)) {
        $local_file = strtr($original_path, [
            'public:/' => PublicStream::basePath(),
        ]);
    }
    else {
        $local_file = $this->themeManager
            ->getActiveTheme()
            ->getPath() . '/' . $default;
    }
    $description = $this->t('Examples: <code>@implicit-public-file</code> (for a file in the public filesystem), <code>@explicit-file</code>, or <code>@local-file</code>.', [
        '@implicit-public-file' => $friendly_path ?? $default,
        '@explicit-file' => StreamWrapperManager::getScheme($original_path) !== FALSE ? $original_path : 'public://' . $default,
        '@local-file' => $local_file,
    ]);
    $allowed = 'png jpg jpeg';
    $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("If you don't have direct file access to the server, use this field to upload your logo. Recommended image dimension %width x %height pixels.", [
            '%width' => $config->get('logo.max.width'),
            '%height' => $config->get('logo.max.height'),
        ]),
        '#upload_validators' => $upload_validators,
        '#cardinality' => 1,
    ];
    $form['logo']['custom']['logo_path'] = [
        '#type' => 'textfield',
        '#title' => $this->t('Path to custom logo'),
        '#default_value' => $default_path,
        '#description' => $description,
        '#config_target' => 'navigation.settings:logo.path',
    ];
    $form['logo']['custom']['logo_upload'] = [
        '#type' => 'file',
        '#title' => $this->t('Upload logo image'),
        '#description' => $this->renderer
            ->renderInIsolation($file_upload_help),
        '#upload_validators' => $upload_validators,
    ];
    return parent::buildForm($form, $form_state);
}

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