You are here

class JavascriptLibrariesEditForm in JavaScript Libraries Manager 8

Hierarchy

Expanded class hierarchy of JavascriptLibrariesEditForm

File

src/Form/JavascriptLibrariesEditForm.php, line 14
Contains \Drupal\javascript_libraries\Form\JavascriptLibrariesEditForm.

Namespace

Drupal\javascript_libraries\Form
View source
class JavascriptLibrariesEditForm extends FormBase {

  /**
   * {@inheritdoc}
   */
  public function getFormId() {
    return 'javascript_libraries_edit_form';
  }
  public function buildForm(array $form, \Drupal\Core\Form\FormStateInterface $form_state, $library = NULL) {
    $form['#library'] = $library;
    if (!isset($form['#library']['weight'])) {
      $form['#library']['weight'] = 5;
    }
    $form['library_type'] = [
      '#type' => 'radios',
      '#title' => t('Source'),
      '#required' => TRUE,
      '#options' => [
        'external' => t('External URL'),
        'file' => t('File'),
      ],
      '#default_value' => isset($library['type']) ? $library['type'] : 'external',
      '#disabled' => isset($library['type']),
    ];
    $external_access = empty($library['type']) || $library['type'] == 'external';
    $form['external_url'] = [
      '#type' => 'textfield',
      '#title' => t('URL'),
      '#description' => t('Enter the full URL of a JavaScript library. URL must start with http:// or https:// and end with .js or .txt.'),
      '#states' => [
        'visible' => [
          ':input[name="library_type"]' => [
            'value' => 'external',
          ],
        ],
      ],
      '#default_value' => isset($library['uri']) ? $library['uri'] : '',
      '#access' => $external_access,
    ];
    $form['cache_external'] = [
      '#type' => 'checkbox',
      '#title' => t('Cache script locally'),
      '#description' => t('This option only takes effect if JavaScript aggregation is enabled. You must verify that the license or terms of service for the script permit local caching and aggregation.'),
      '#default_value' => isset($library['cache']) ? $library['cache'] : FALSE,
      '#access' => $external_access,
      '#states' => [
        'visible' => [
          ':input[name="library_type"]' => [
            'value' => 'external',
          ],
        ],
      ],
    ];
    $file_access = empty($library['type']) || $library['type'] == 'file';
    $form['js_file_upload'] = [
      '#type' => 'managed_file',
      '#title' => t('File'),
      '#description' => t('Upload a JavaScript file from your computer. It must end in .js or .txt. It will be renamed to have a .txt extension.'),
      '#upload_location' => 'public://javascript_libraries',
      '#upload_validators' => [
        'file_validate_extensions' => [
          0 => 'js txt',
        ],
      ],
      '#states' => [
        'visible' => [
          ':input[name="library_type"]' => [
            'value' => 'file',
          ],
        ],
      ],
      '#default_value' => $file_access && isset($library['fid']) ? $library['fid'] : NULL,
      '#access' => empty($library['type']) || $library['type'] == 'file',
    ];
    $form['scope'] = [
      '#type' => 'select',
      '#title' => t('Region on page'),
      '#required' => TRUE,
      '#options' => [
        'header' => t('Head'),
        'footer' => t('Footer'),
        'disabled' => '<' . t('Disabled') . '>',
      ],
      '#default_value' => isset($library['scope']) ? $library['scope'] : 'footer',
    ];
    $form['name'] = [
      '#type' => 'textfield',
      '#title' => t('Library description'),
      '#default_value' => isset($library['name']) ? $library['name'] : '',
      '#description' => 'Defaults to the file name or URL.',
    ];
    $form['actions']['submit'] = [
      '#type' => 'submit',
      '#value' => t('Save'),
    ];
    $form['actions']['cancel'] = [
      '#type' => 'link',
      '#title' => t('Cancel'),
      '#href' => 'admin/config/system/javascript-libraries/custom',
    ];
    return $form;
  }
  public function validateForm(array &$form, \Drupal\Core\Form\FormStateInterface $form_state) {
    switch ($form_state
      ->getValue([
      'library_type',
    ])) {
      case 'external':
        _javascript_libraries_url_validate($form, $form_state);
        break;
      case 'file':
        _javascript_libraries_file_validate($form, $form_state);
        break;
    }

    // Trim the name/description:
    $form_state
      ->setValue([
      'name',
    ], trim($form_state
      ->getValue([
      'name',
    ])));
  }
  public function submitForm(array &$form, \Drupal\Core\Form\FormStateInterface $form_state) {
    switch ($form_state
      ->getValue([
      'library_type',
    ])) {
      case 'external':
        if (empty($form['#library']['id'])) {

          // New URL
          $form['#library']['id'] = 'ext-' . db_next_id();
        }

        // @FIXME
        // Could not extract the default value because it is either indeterminate, or
        // not scalar. You'll need to provide a default value in
        // config/install/javascript_libraries.settings.yml and config/schema/javascript_libraries.schema.yml.
        $custom = \Drupal::config('javascript_libraries.settings')
          ->get('javascript_libraries_custom_libraries');
        if (strlen($form_state
          ->getValue([
          'name',
        ])) == 0) {
          $parts = explode('/', $form_state
            ->getValue([
            'external_url',
          ]));
          $form_state
            ->setValue([
            'name',
          ], '... /' . end($parts));
        }
        $custom[$form['#library']['id']] = [
          'id' => $form['#library']['id'],
          'type' => 'external',
          'scope' => $form_state
            ->getValue([
            'scope',
          ]),
          'name' => $form_state
            ->getValue([
            'name',
          ]),
          'weight' => $form['#library']['weight'],
          'uri' => $form_state
            ->getValue([
            'external_url',
          ]),
          'cache' => $form_state
            ->getValue([
            'cache_external',
          ]),
        ];
        \Drupal::configFactory()
          ->getEditable('javascript_libraries.settings')
          ->set('javascript_libraries_custom_libraries', $custom)
          ->save();
        break;
      case 'file':
        _javascript_libraries_file_submit($form, $form_state);

        // Change query-strings on css/js files to enforce reload for all users.
        javascript_libraries_js_cache_clear();
        break;
    }
    drupal_set_message('Your library has been added. Please configure the region and weight.');
    $form_state
      ->set([
      'redirect',
    ], 'admin/config/system/javascript-libraries/custom');
  }

}

Members

Namesort descending Modifiers Type Description Overrides
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
FormBase::$configFactory protected property The config factory. 1
FormBase::$requestStack protected property The request stack. 1
FormBase::$routeMatch protected property The route match.
FormBase::config protected function Retrieves a configuration object.
FormBase::configFactory protected function Gets the config factory for this form. 1
FormBase::container private function Returns the service container.
FormBase::create public static function Instantiates a new instance of this class. Overrides ContainerInjectionInterface::create 87
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.
JavascriptLibrariesEditForm::buildForm public function Form constructor. Overrides FormInterface::buildForm
JavascriptLibrariesEditForm::getFormId public function Returns a unique string identifying the form. Overrides FormInterface::getFormId
JavascriptLibrariesEditForm::submitForm public function Form submission handler. Overrides FormInterface::submitForm
JavascriptLibrariesEditForm::validateForm public function Form validation handler. Overrides FormBase::validateForm
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.