Loading mask.module +2 −4 Original line number Diff line number Diff line Loading @@ -9,9 +9,7 @@ use Drupal\Core\Field\WidgetInterface; use Drupal\Core\Field\FieldDefinitionInterface; use Drupal\Core\Form\FormStateInterface; use Drupal\mask\Helper\ElementHelper; // CDN URL for the jQuery Mask Plugin. define('MASK_PLUGIN_CDN_URL', 'https://cdnjs.cloudflare.com/ajax/libs/jquery.mask/1.14.15/jquery.mask.min.js'); use Drupal\mask\Mask; /** * Implements hook_element_info_alter(). Loading Loading @@ -79,7 +77,7 @@ function mask_library_info_alter(&$libraries, $extension) { $config = \Drupal::config('mask.settings'); if ($config->get('use_cdn')) { // Serves from CDN. $libraries['mask_plugin']['js'][MASK_PLUGIN_CDN_URL] = [ $libraries['mask_plugin']['js'][Mask::getCdnUrl()] = [ 'type' => 'external', 'minified' => TRUE, ]; Loading src/Form/SettingsForm.php +58 −10 Original line number Diff line number Diff line Loading @@ -2,14 +2,63 @@ namespace Drupal\mask\Form; use Drupal\Core\Asset\LibraryDiscoveryInterface; use Drupal\Core\Config\ConfigFactoryInterface; use Drupal\Core\File\FileSystemInterface; use Drupal\Core\Form\ConfigFormBase; use Drupal\Core\Form\FormStateInterface; use Drupal\mask\Mask; use GuzzleHttp\ClientInterface; use Symfony\Component\DependencyInjection\ContainerInterface; /** * Module settings form. */ class SettingsForm extends ConfigFormBase { /** * The HTTP client. * * @var \GuzzleHttp\ClientInterface */ protected $httpClient; /** * The file system service. * * @var \Drupal\Core\File\FileSystemInterface */ protected $fileSystem; /** * The library discovery service. * * @var \Drupal\Core\Asset\LibraryDiscoveryInterface */ protected $libraryDiscovery; /** * {@inheritdoc} */ public function __construct(ConfigFactoryInterface $config_factory, ClientInterface $client, FileSystemInterface $file_system, LibraryDiscoveryInterface $library_discovery) { parent::__construct($config_factory); $this->httpClient = $client; $this->fileSystem = $file_system; $this->libraryDiscovery = $library_discovery; } /** * {@inheritdoc} */ public static function create(ContainerInterface $container) { return new static( $container->get('config.factory'), $container->get('http_client'), $container->get('file_system'), $container->get('library.discovery') ); } /** * {@inheritdoc} */ Loading Loading @@ -119,12 +168,11 @@ class SettingsForm extends ConfigFormBase { else { // Tries to download from the CDN defined in the mask.libraries.yml // file. $http_client = \Drupal::httpClient(); $response = $http_client->get(MASK_PLUGIN_CDN_URL); $response = $this->httpClient->get(Mask::getCdnUrl()); if ($response->getStatusCode() == 200) { // Saves the downloaded file. $destination = 'public://jquery.mask.min.js'; $plugin_path = \Drupal::service('file_system')->saveData($response->getBody(), $destination); $plugin_path = $this->fileSystem->saveData($response->getBody(), $destination); $form_state->setValue('plugin_path', $plugin_path); } if (!$plugin_path) { Loading Loading @@ -154,8 +202,8 @@ class SettingsForm extends ConfigFormBase { $empty_pattern = []; $duplicates = []; foreach ($symbol_values as $delta => $value) { $symbol = isset($value['symbol']) ? $value['symbol'] : ''; $pattern = isset($value['pattern']) ? $value['pattern'] : ''; $symbol = $value['symbol'] ?? ''; $pattern = $value['pattern'] ?? ''; if ($symbol !== '') { // Count this delta for the symbol. $duplicates[$symbol][] = $delta; Loading Loading @@ -213,7 +261,7 @@ class SettingsForm extends ConfigFormBase { $changed_use_cdn = $form['use_cdn']['#default_value'] != $form_state->getValue('use_cdn'); $changed_plugin_path = $form['plugin_path']['#default_value'] != $form_state->getValue('plugin_path'); if ($changed_use_cdn || $changed_plugin_path) { \Drupal::service('library.discovery')->clearCachedDefinitions(); $this->libraryDiscovery->clearCachedDefinitions(); } parent::submitForm($form, $form_state); Loading src/Mask.php 0 → 100644 +20 −0 Original line number Diff line number Diff line <?php namespace Drupal\mask; /** * The mask class. */ class Mask { /** * Return the jQuery Mask CDN Url. * * @return string * The CDN url. */ public static function getCdnUrl(): string { return 'https://cdnjs.cloudflare.com/ajax/libs/jquery.mask/1.14.15/jquery.mask.min.js'; } } src/Plugin/FieldWidgetPlugin.php +8 −3 Original line number Diff line number Diff line Loading @@ -45,7 +45,12 @@ class FieldWidgetPlugin extends PluginBase implements FieldWidgetPluginInterface * {@inheritdoc} */ public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) { return new static($configuration, $plugin_id, $plugin_definition, $container->get('config.factory')); return new static( $configuration, $plugin_id, $plugin_definition, $container->get('config.factory') ); } /** Loading Loading @@ -169,8 +174,8 @@ class FieldWidgetPlugin extends PluginBase implements FieldWidgetPluginInterface $element['#rows'][] = [ $symbol, $options['pattern'], isset($options['fallback']) ? $options['fallback'] : '', isset($options['description']) ? $options['description'] : '', $options['fallback'] ?? '', $options['description'] ?? '', empty($options['optional']) ? $this->t('No') : $this->t('Yes'), empty($options['recursive']) ? $this->t('No') : $this->t('Yes'), ]; Loading src/Plugin/FieldWidgetPluginInterface.php +1 −1 Original line number Diff line number Diff line Loading @@ -46,7 +46,7 @@ interface FieldWidgetPluginInterface { * * @see hook_field_widget_third_party_settings_form() */ public function fieldWidgetThirdPartySettingsForm(WidgetInterface $plugin, FieldDefinitionInterface $field_definition, $form_mode, array $form, FormStateInterface $form_state); public function fieldWidgetThirdPartySettingsForm(WidgetInterface $plugin, FieldDefinitionInterface $field_definition, string $form_mode, array $form, FormStateInterface $form_state); /** * Alters the field widget settings summary. Loading Loading
mask.module +2 −4 Original line number Diff line number Diff line Loading @@ -9,9 +9,7 @@ use Drupal\Core\Field\WidgetInterface; use Drupal\Core\Field\FieldDefinitionInterface; use Drupal\Core\Form\FormStateInterface; use Drupal\mask\Helper\ElementHelper; // CDN URL for the jQuery Mask Plugin. define('MASK_PLUGIN_CDN_URL', 'https://cdnjs.cloudflare.com/ajax/libs/jquery.mask/1.14.15/jquery.mask.min.js'); use Drupal\mask\Mask; /** * Implements hook_element_info_alter(). Loading Loading @@ -79,7 +77,7 @@ function mask_library_info_alter(&$libraries, $extension) { $config = \Drupal::config('mask.settings'); if ($config->get('use_cdn')) { // Serves from CDN. $libraries['mask_plugin']['js'][MASK_PLUGIN_CDN_URL] = [ $libraries['mask_plugin']['js'][Mask::getCdnUrl()] = [ 'type' => 'external', 'minified' => TRUE, ]; Loading
src/Form/SettingsForm.php +58 −10 Original line number Diff line number Diff line Loading @@ -2,14 +2,63 @@ namespace Drupal\mask\Form; use Drupal\Core\Asset\LibraryDiscoveryInterface; use Drupal\Core\Config\ConfigFactoryInterface; use Drupal\Core\File\FileSystemInterface; use Drupal\Core\Form\ConfigFormBase; use Drupal\Core\Form\FormStateInterface; use Drupal\mask\Mask; use GuzzleHttp\ClientInterface; use Symfony\Component\DependencyInjection\ContainerInterface; /** * Module settings form. */ class SettingsForm extends ConfigFormBase { /** * The HTTP client. * * @var \GuzzleHttp\ClientInterface */ protected $httpClient; /** * The file system service. * * @var \Drupal\Core\File\FileSystemInterface */ protected $fileSystem; /** * The library discovery service. * * @var \Drupal\Core\Asset\LibraryDiscoveryInterface */ protected $libraryDiscovery; /** * {@inheritdoc} */ public function __construct(ConfigFactoryInterface $config_factory, ClientInterface $client, FileSystemInterface $file_system, LibraryDiscoveryInterface $library_discovery) { parent::__construct($config_factory); $this->httpClient = $client; $this->fileSystem = $file_system; $this->libraryDiscovery = $library_discovery; } /** * {@inheritdoc} */ public static function create(ContainerInterface $container) { return new static( $container->get('config.factory'), $container->get('http_client'), $container->get('file_system'), $container->get('library.discovery') ); } /** * {@inheritdoc} */ Loading Loading @@ -119,12 +168,11 @@ class SettingsForm extends ConfigFormBase { else { // Tries to download from the CDN defined in the mask.libraries.yml // file. $http_client = \Drupal::httpClient(); $response = $http_client->get(MASK_PLUGIN_CDN_URL); $response = $this->httpClient->get(Mask::getCdnUrl()); if ($response->getStatusCode() == 200) { // Saves the downloaded file. $destination = 'public://jquery.mask.min.js'; $plugin_path = \Drupal::service('file_system')->saveData($response->getBody(), $destination); $plugin_path = $this->fileSystem->saveData($response->getBody(), $destination); $form_state->setValue('plugin_path', $plugin_path); } if (!$plugin_path) { Loading Loading @@ -154,8 +202,8 @@ class SettingsForm extends ConfigFormBase { $empty_pattern = []; $duplicates = []; foreach ($symbol_values as $delta => $value) { $symbol = isset($value['symbol']) ? $value['symbol'] : ''; $pattern = isset($value['pattern']) ? $value['pattern'] : ''; $symbol = $value['symbol'] ?? ''; $pattern = $value['pattern'] ?? ''; if ($symbol !== '') { // Count this delta for the symbol. $duplicates[$symbol][] = $delta; Loading Loading @@ -213,7 +261,7 @@ class SettingsForm extends ConfigFormBase { $changed_use_cdn = $form['use_cdn']['#default_value'] != $form_state->getValue('use_cdn'); $changed_plugin_path = $form['plugin_path']['#default_value'] != $form_state->getValue('plugin_path'); if ($changed_use_cdn || $changed_plugin_path) { \Drupal::service('library.discovery')->clearCachedDefinitions(); $this->libraryDiscovery->clearCachedDefinitions(); } parent::submitForm($form, $form_state); Loading
src/Mask.php 0 → 100644 +20 −0 Original line number Diff line number Diff line <?php namespace Drupal\mask; /** * The mask class. */ class Mask { /** * Return the jQuery Mask CDN Url. * * @return string * The CDN url. */ public static function getCdnUrl(): string { return 'https://cdnjs.cloudflare.com/ajax/libs/jquery.mask/1.14.15/jquery.mask.min.js'; } }
src/Plugin/FieldWidgetPlugin.php +8 −3 Original line number Diff line number Diff line Loading @@ -45,7 +45,12 @@ class FieldWidgetPlugin extends PluginBase implements FieldWidgetPluginInterface * {@inheritdoc} */ public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) { return new static($configuration, $plugin_id, $plugin_definition, $container->get('config.factory')); return new static( $configuration, $plugin_id, $plugin_definition, $container->get('config.factory') ); } /** Loading Loading @@ -169,8 +174,8 @@ class FieldWidgetPlugin extends PluginBase implements FieldWidgetPluginInterface $element['#rows'][] = [ $symbol, $options['pattern'], isset($options['fallback']) ? $options['fallback'] : '', isset($options['description']) ? $options['description'] : '', $options['fallback'] ?? '', $options['description'] ?? '', empty($options['optional']) ? $this->t('No') : $this->t('Yes'), empty($options['recursive']) ? $this->t('No') : $this->t('Yes'), ]; Loading
src/Plugin/FieldWidgetPluginInterface.php +1 −1 Original line number Diff line number Diff line Loading @@ -46,7 +46,7 @@ interface FieldWidgetPluginInterface { * * @see hook_field_widget_third_party_settings_form() */ public function fieldWidgetThirdPartySettingsForm(WidgetInterface $plugin, FieldDefinitionInterface $field_definition, $form_mode, array $form, FormStateInterface $form_state); public function fieldWidgetThirdPartySettingsForm(WidgetInterface $plugin, FieldDefinitionInterface $field_definition, string $form_mode, array $form, FormStateInterface $form_state); /** * Alters the field widget settings summary. Loading