You are here

class FileSettingsForm in File Entity (fieldable files) 8.2

Class FileSettingsForm @package Drupal\file_entity\Form

Hierarchy

Expanded class hierarchy of FileSettingsForm

1 string reference to 'FileSettingsForm'
file_entity.routing.yml in ./file_entity.routing.yml
file_entity.routing.yml

File

src/Form/FileSettingsForm.php, line 13

Namespace

Drupal\file_entity\Form
View source
class FileSettingsForm extends ConfigFormBase {

  /**
   * Returns a unique string identifying the form.
   *
   * @return string
   *   The unique string identifying the form.
   */
  public function getFormId() {
    return 'file_settings';
  }

  /**
   * {@inheritdoc}
   */
  protected function getEditableConfigNames() {
    return [
      'file_entity.settings',
    ];
  }

  /**
   * {@inheritdoc}
   */
  public function buildForm(array $form, FormStateInterface $form_state) {
    $form['max_filesize'] = array(
      '#type' => 'textfield',
      '#title' => t('Maximum upload size'),
      '#default_value' => \Drupal::config('file_entity.settings')
        ->get('max_filesize'),
      '#description' => t('Enter a value like "512" (bytes), "80 KB" (kilobytes) or "50 MB" (megabytes) in order to restrict the allowed file size. If left empty the file sizes will be limited only by PHP\'s maximum post and file upload sizes (current max limit <strong>%limit</strong>).', array(
        '%limit' => format_size(Environment::getUploadMaxSize()),
      )),
      '#element_validate' => [
        '\\Drupal\\file\\Plugin\\Field\\FieldType\\FileItem::validateMaxFilesize',
      ],
      '#size' => 10,
    );
    $form['default_allowed_extensions'] = array(
      '#type' => 'textfield',
      '#title' => t('Default allowed file extensions'),
      '#default_value' => \Drupal::config('file_entity.settings')
        ->get('default_allowed_extensions'),
      '#description' => t('Separate extensions with a space or comma and do not include the leading dot.'),
      '#element_validate' => [
        '\\Drupal\\file\\Plugin\\Field\\FieldType\\FileItem::validateExtensions',
      ],
      '#maxlength' => NULL,
    );
    $form['file_entity_alt'] = array(
      '#type' => 'textfield',
      '#title' => t('Alt attribute'),
      '#description' => t('The text to use as value for the <em>img</em> tag <em>alt</em> attribute.'),
      '#default_value' => \Drupal::config('file_entity.settings')
        ->get('alt'),
    );
    $form['file_entity_title'] = array(
      '#type' => 'textfield',
      '#title' => t('Title attribute'),
      '#description' => t('The text to use as value for the <em>img</em> tag <em>title</em> attribute.'),
      '#default_value' => \Drupal::config('file_entity.settings')
        ->get('title'),
    );

    // Provide default token values.
    if (\Drupal::moduleHandler()
      ->moduleExists('token')) {
      $form['token_help'] = array(
        '#theme' => 'token_tree_link',
        '#token_types' => array(
          'file',
        ),
      );
      $form['file_entity_alt']['#description'] .= t('This field supports tokens. Default tokens depend on the <a href=":token">Token module</a> to work correctly. The ":value" version of the token (just raw value, no markup) should be used for performance and to avoid theme issues.', [
        ':token' => 'https://drupal.org/project/token',
      ]);
      $form['file_entity_title']['#description'] .= t('This field supports tokens. Default tokens depend on the <a href=":token">Token module</a> to work correctly. The ":value" version of the token (just raw value, no markup) should be used for performance and to avoid theme issues', [
        ':token' => 'https://drupal.org/project/token',
      ]);
    }
    $form['file_upload_wizard'] = array(
      '#type' => 'fieldset',
      '#title' => t('File upload wizard'),
      '#collapsible' => TRUE,
      '#collapsed' => FALSE,
      '#description' => t('Configure the steps available when uploading a new file.'),
    );
    $form['file_upload_wizard']['wizard_skip_file_type'] = array(
      '#type' => 'checkbox',
      '#title' => t('Skip filetype selection.'),
      '#default_value' => \Drupal::config('file_entity.settings')
        ->get('wizard_skip_file_type'),
      '#description' => t('The file type selection step is only available if the uploaded file falls into two or more file types. If this step is skipped, files with no available file type or two or more file types will not be assigned a file type.'),
    );
    $form['file_upload_wizard']['wizard_skip_scheme'] = array(
      '#type' => 'checkbox',
      '#title' => t('Skip scheme selection.'),
      '#default_value' => \Drupal::config('file_entity.settings')
        ->get('wizard_skip_scheme'),
      '#description' => t('The scheme selection step is only available if two or more file destinations, such as public local files served by the webserver and private local files served by Drupal, are available. If this step is skipped, files will automatically be saved using the default download method.'),
    );
    $form['file_upload_wizard']['wizard_skip_fields'] = array(
      '#type' => 'checkbox',
      '#title' => t('Skip available fields.'),
      '#default_value' => \Drupal::config('file_entity.settings')
        ->get('wizard_skip_fields'),
      '#description' => t('The field selection step is only available if the file type the file belongs to has any available fields. If this step is skipped, any fields on the file will be left blank.'),
    );
    $form['actions']['submit'] = array(
      '#type' => 'submit',
      '#value' => t('Save'),
    );
    return $form;
  }

  /**
   * {@inheritdoc}
   */
  public function validateForm(array &$form, FormStateInterface $form_state) {

    // @TODO: Validation?
  }

  /**
   * {@inheritdoc}
   */
  public function submitForm(array &$form, FormStateInterface $form_state) {
    $this
      ->config('file_entity.settings')
      ->set('max_filesize', $form_state
      ->getValue('max_filesize'))
      ->set('default_allowed_extensions', $form_state
      ->getValue('default_allowed_extensions'))
      ->set('alt', $form_state
      ->getValue('file_entity_alt'))
      ->set('title', $form_state
      ->getValue('file_entity_title'))
      ->set('wizard_skip_file_type', $form_state
      ->getValue('wizard_skip_file_type'))
      ->set('wizard_skip_scheme', $form_state
      ->getValue('wizard_skip_scheme'))
      ->set('wizard_skip_fields', $form_state
      ->getValue('wizard_skip_fields'))
      ->save();
    $this
      ->messenger()
      ->addMessage(t('File Settings have been succesfully saved.'));
  }

}

Members

Namesort descending Modifiers Type Description Overrides
ConfigFormBase::create public static function Instantiates a new instance of this class. Overrides FormBase::create 13
ConfigFormBase::__construct public function Constructs a \Drupal\system\ConfigFormBase object. 11
ConfigFormBaseTrait::config protected function Retrieves a configuration object.
DependencySerializationTrait::$_entityStorages protected property An array of entity type IDs keyed by the property name of their storages.
DependencySerializationTrait::$_serviceIds protected property An array of service IDs keyed by property name used for serialization.
DependencySerializationTrait::__sleep public function 1
DependencySerializationTrait::__wakeup public function 2
FileSettingsForm::buildForm public function Form constructor. Overrides ConfigFormBase::buildForm
FileSettingsForm::getEditableConfigNames protected function Gets the configuration names that will be editable. Overrides ConfigFormBaseTrait::getEditableConfigNames
FileSettingsForm::getFormId public function Returns a unique string identifying the form. Overrides FormInterface::getFormId
FileSettingsForm::submitForm public function Form submission handler. Overrides ConfigFormBase::submitForm
FileSettingsForm::validateForm public function Form validation handler. Overrides FormBase::validateForm
FormBase::$configFactory protected property The config factory. 1
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. 1
FormBase::container private function Returns the service container.
FormBase::currentUser protected function Gets the current user.
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. Overrides UrlGeneratorTrait::redirect
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.
LinkGeneratorTrait::$linkGenerator protected property The link generator. 1
LinkGeneratorTrait::getLinkGenerator Deprecated protected function Returns the link generator.
LinkGeneratorTrait::l Deprecated protected function Renders a link to a route given a route name and its parameters.
LinkGeneratorTrait::setLinkGenerator Deprecated public function Sets the link generator service.
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. 29
MessengerTrait::messenger public function Gets the messenger. 29
MessengerTrait::setMessenger public function Sets the messenger.
RedirectDestinationTrait::$redirectDestination protected property The redirect destination service. 1
RedirectDestinationTrait::getDestinationArray protected function Prepares a 'destination' 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.
StringTranslationTrait::$stringTranslation protected property The string translation service. 1
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.
UrlGeneratorTrait::$urlGenerator protected property The url generator.
UrlGeneratorTrait::getUrlGenerator Deprecated protected function Returns the URL generator service.
UrlGeneratorTrait::setUrlGenerator Deprecated public function Sets the URL generator service.
UrlGeneratorTrait::url Deprecated protected function Generates a URL or path for a specific route based on the given parameters.