Skip to content

Instantly share code, notes, and snippets.

@ahebrank
Created January 27, 2024 23:21
Show Gist options
  • Save ahebrank/0adac5c6bca5bbcd3f35174ded033984 to your computer and use it in GitHub Desktop.
Save ahebrank/0adac5c6bca5bbcd3f35174ded033984 to your computer and use it in GitHub Desktop.
diff --git a/README.txt b/README.md
similarity index 100%
rename from README.txt
rename to README.md
diff --git a/css/soft_length_limit.css b/css/soft_length_limit.css
index 42c031e..fb5b38d 100644
--- a/css/soft_length_limit.css
+++ b/css/soft_length_limit.css
@@ -14,8 +14,8 @@ div.soft-length-limit-tooltip.description {
/* Min/Enhanced tooltip */
.fieldset-wrapper .form-item .soft-length-limit-tooltip.min-style-tooltip,
.field-type-text .form-item .soft-length-limit-tooltip.min-style-tooltip,
-.form-item.form-type-textfield .soft-length-limit-tooltip.min-style-tooltip,
-.form-item.form-type-textarea .soft-length-limit-tooltip.min-style-tooltip {
+.form-item.form-type--textfield .soft-length-limit-tooltip.min-style-tooltip,
+.form-item.form-type--textarea .soft-length-limit-tooltip.min-style-tooltip {
background-color: transparent;
background-image: url(../img/ico_socpub_confirm.png);
background-position: left top;
@@ -30,20 +30,20 @@ div.soft-length-limit-tooltip.description {
right: 10px;
}
-.form-item.form-type-textfield .soft-length-limit-tooltip.min-style-tooltip {
+.form-item.form-type--textfield .soft-length-limit-tooltip.min-style-tooltip {
margin-left: 5px;
}
.fieldset-wrapper .form-item .soft-length-limit-tooltip.min-style-tooltip.under-min,
-.form-item.form-type-textfield .soft-length-limit-tooltip.min-style-tooltip.under-min,
+.form-item.form-type--textfield .soft-length-limit-tooltip.min-style-tooltip.under-min,
.field-type-text .form-item .soft-length-limit-tooltip.min-style-tooltip.under-min,
-.form-item.form-type-textarea .soft-length-limit-tooltip.min-style-tooltip.under-min {
+.form-item.form-type--textarea .soft-length-limit-tooltip.min-style-tooltip.under-min {
background-image: url(../img/ico_socpub_alert.png);
}
.fieldset-wrapper .form-item .soft-length-limit-tooltip.min-style-tooltip.exceeded,
-.form-item.form-type-textfield .soft-length-limit-tooltip.min-style-tooltip.exceeded,
+.form-item.form-type--textfield .soft-length-limit-tooltip.min-style-tooltip.exceeded,
.field-type-text .form-item .soft-length-limit-tooltip.min-style-tooltip.exceeded,
-.form-item.form-type-textarea .soft-length-limit-tooltip.min-style-tooltip.exceeded {
+.form-item.form-type--textarea .soft-length-limit-tooltip.min-style-tooltip.exceeded {
background-image: url(../img/ico_socpub_alert.png);
}
diff --git a/modules/soft_length_limit_migrate/soft_length_limit_migrate.info.yml b/modules/soft_length_limit_migrate/soft_length_limit_migrate.info.yml
new file mode 100644
index 0000000..e0a8a18
--- /dev/null
+++ b/modules/soft_length_limit_migrate/soft_length_limit_migrate.info.yml
@@ -0,0 +1,5 @@
+name: Soft Length Limit Migrate
+type: module
+description: Migration Support for soft_length_limit
+package: Migrate
+core_version_requirement: ^9 || ^10
diff --git a/modules/soft_length_limit_migrate/soft_length_limit_migrate.module b/modules/soft_length_limit_migrate/soft_length_limit_migrate.module
new file mode 100644
index 0000000..85e6276
--- /dev/null
+++ b/modules/soft_length_limit_migrate/soft_length_limit_migrate.module
@@ -0,0 +1,39 @@
+<?php
+
+/**
+ * @file
+ * Soft Length Limit Migrate module.
+ */
+
+use Drupal\migrate\Plugin\MigrateSourceInterface;
+use Drupal\migrate\Plugin\MigrationInterface;
+use Drupal\migrate\Row;
+
+/**
+ * Implements hook_migrate_prepare_row().
+ */
+function soft_length_limit_migrate_migrate_prepare_row(Row $row, MigrateSourceInterface $source, MigrationInterface $migration) {
+ // Add migrate support to migrate D7 settings into D8 fields.
+ $text_fields = [
+ 'text_textarea',
+ 'text_textfield',
+ 'text_textarea_with_summary',
+ 'text_long',
+ ];
+
+ if ($source->getPluginId() !== 'd7_field_instance_per_form_display' || !in_array($row->getSource()['type'], $text_fields)) {
+ return;
+ }
+
+ $widget_settings = $row->getSourceProperty('widget')['settings'];
+
+ if (!empty($widget_settings['soft_length_limit']) || !empty($widget_settings['soft_length_minimum'])) {
+ $constants = $row->getSourceProperty('constants');
+ $constants['third_party_settings']['soft_length_limit'] = [
+ 'max_limit' => $widget_settings['soft_length_limit'],
+ 'minimum_limit' => $widget_settings['soft_length_minimum'],
+ 'style_select' => $widget_settings['soft_length_style_select'],
+ ];
+ $row->setSourceProperty('constants', $constants);
+ }
+}
diff --git a/phpcs.xml b/phpcs.xml
new file mode 100644
index 0000000..e8433b4
--- /dev/null
+++ b/phpcs.xml
@@ -0,0 +1,14 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<ruleset name="phpcs-standard">
+ <description>Codestyle ruleset for Drupal</description>
+
+ <!-- Specify standards. These assume "composer global require drupal/coder"-->
+ <rule ref="Drupal"/>
+ <rule ref="DrupalPractice"/>
+
+ <!-- Set ignore extensions. -->
+ <!-- @todo remove .css to check also the css files. -->
+ <!-- @see https://www.drupal.org/node/2867601#comment-12075633 -->
+ <arg name="ignore" value="*.css,*.md,*.txt"/>
+
+</ruleset>
diff --git a/soft_length_limit.module b/soft_length_limit.module
index 60fe4e0..056a9af 100644
--- a/soft_length_limit.module
+++ b/soft_length_limit.module
@@ -10,55 +10,14 @@ declare(strict_types = 1);
use Drupal\Core\Field\FieldDefinitionInterface;
use Drupal\Core\Field\WidgetInterface;
use Drupal\Core\Form\FormStateInterface;
-use Drupal\migrate\Plugin\MigrateSourceInterface;
-use Drupal\migrate\Plugin\MigrationInterface;
-use Drupal\migrate\Row;
-
-/**
- * Returns the widget settings that can be used for a soft_length widget.
- *
- * @return array
- * An array of settings and default values for each textfield type.
- */
-function _soft_length_widget_settings(string $plugin_id): array {
- $settings = [
- 'string_textfield' => [
- 'max_limit' => TRUE,
- 'minimum_limit' => TRUE,
- 'style_select' => TRUE,
- ],
- 'string_textarea' => [
- 'max_limit' => TRUE,
- 'minimum_limit' => TRUE,
- 'style_select' => TRUE,
- ],
- 'text_textfield' => [
- 'max_limit' => TRUE,
- 'minimum_limit' => TRUE,
- 'style_select' => TRUE,
- ],
- 'text_textarea' => [
- 'max_limit' => TRUE,
- 'minimum_limit' => TRUE,
- 'style_select' => TRUE,
- ],
- 'text_textarea_with_summary' => [
- 'max_limit' => TRUE,
- 'minimum_limit' => TRUE,
- 'style_select' => TRUE,
- ],
- ];
-
- return $settings[$plugin_id] ?? [];
-}
+use Drupal\soft_length_limit\SoftLengthLimitWidgetSettings;
/**
* Implements hook_field_widget_third_party_settings_form().
*/
function soft_length_limit_field_widget_third_party_settings_form(WidgetInterface $plugin, FieldDefinitionInterface $field_definition, $form_mode, $form, FormStateInterface $form_state) {
- $plugin_id = $plugin->getPluginId();
- if (!$allowed_settings = _soft_length_widget_settings($plugin_id)) {
- return NULL;
+ if (!$allowed_settings = SoftLengthLimitWidgetSettings::allowedForPlugin($plugin->getPluginId())) {
+ return;
}
$element = [];
@@ -87,9 +46,9 @@ function soft_length_limit_field_widget_third_party_settings_form(WidgetInterfac
if ($allowed_settings['style_select']) {
$element['style_select'] = [
'#type' => 'checkbox',
- '#title' => t('Enable enhanced view'),
+ '#title' => t('Minimal limit message'),
'#default_value' => $plugin->getThirdPartySetting('soft_length_limit', 'style_select'),
- '#description' => t('Check this to enable an enhanced view of soft length states.'),
+ '#description' => t('Enable a minimal view of soft length states.'),
'#weight' => -1,
];
}
@@ -103,10 +62,8 @@ function soft_length_limit_field_widget_third_party_settings_form(WidgetInterfac
function soft_length_limit_field_widget_settings_summary_alter(&$summary, $context) {
/* @var \Drupal\Core\Field\WidgetInterface $widget */
$widget = $context['widget'];
- $plugin_id = $widget->getPluginId();
-
- if (!$allowed_settings = _soft_length_widget_settings($plugin_id)) {
- return NULL;
+ if (!$allowed_settings = SoftLengthLimitWidgetSettings::allowedForPlugin($widget->getPluginId())) {
+ return;
}
$max_limit = $allowed_settings['max_limit']
@@ -137,7 +94,7 @@ function soft_length_limit_field_widget_single_element_form_alter(&$element, For
$third_party_settings = $context['widget']->getThirdPartySettings();
if (empty($third_party_settings['soft_length_limit'])) {
- return NULL;
+ return;
}
$sll_config = $third_party_settings['soft_length_limit'];
@@ -155,37 +112,8 @@ function soft_length_limit_field_widget_single_element_form_alter(&$element, For
}
if (isset($element['#type']) && $element['#type'] === 'text_format') {
- $element['#attributes'] = array_merge_recursive($element['#attributes'], $element['value']['#attributes']);
+ $element['#attributes'] = array_merge_recursive($element['#attributes'] ?? [], $element['value']['#attributes'] ?? []);
}
$element['#attached']['library'][] = 'soft_length_limit/soft_length_limit';
}
-
-/**
- * Implements hook_migrate_prepare_row().
- */
-function soft_length_limit_migrate_prepare_row(Row $row, MigrateSourceInterface $source, MigrationInterface $migration) {
- // Add migrate support to migrate D7 settings into D8 fields.
- $text_fields = [
- 'text_textarea',
- 'text_textfield',
- 'text_textarea_with_summary',
- 'text_long',
- ];
-
- if ($source->getPluginId() !== 'd7_field_instance_per_form_display' || !in_array($row->getSource()['type'], $text_fields)) {
- return;
- }
-
- $widget_settings = $row->getSourceProperty('widget')['settings'];
-
- if (!empty($widget_settings['soft_length_limit']) || !empty($widget_settings['soft_length_minimum'])) {
- $constants = $row->getSourceProperty('constants');
- $constants['third_party_settings']['soft_length_limit'] = [
- 'max_limit' => $widget_settings['soft_length_limit'],
- 'minimum_limit' => $widget_settings['soft_length_minimum'],
- 'style_select' => $widget_settings['soft_length_style_select'],
- ];
- $row->setSourceProperty('constants', $constants);
- }
-}
diff --git a/src/SoftLengthLimitWidgetSettings.php b/src/SoftLengthLimitWidgetSettings.php
new file mode 100644
index 0000000..f89b61b
--- /dev/null
+++ b/src/SoftLengthLimitWidgetSettings.php
@@ -0,0 +1,48 @@
+<?php
+
+namespace Drupal\soft_length_limit;
+
+/**
+ * Allowed settings helper.
+ */
+class SoftLengthLimitWidgetSettings {
+
+ /**
+ * Returns the widget settings that can be used for a soft_length widget.
+ *
+ * @return array
+ * An array of settings and default values for each textfield type.
+ */
+ public static function allowedForPlugin(string $plugin_id): array {
+ $settings = [
+ 'string_textfield' => [
+ 'max_limit' => TRUE,
+ 'minimum_limit' => TRUE,
+ 'style_select' => TRUE,
+ ],
+ 'string_textarea' => [
+ 'max_limit' => TRUE,
+ 'minimum_limit' => TRUE,
+ 'style_select' => TRUE,
+ ],
+ 'text_textfield' => [
+ 'max_limit' => TRUE,
+ 'minimum_limit' => TRUE,
+ 'style_select' => TRUE,
+ ],
+ 'text_textarea' => [
+ 'max_limit' => TRUE,
+ 'minimum_limit' => TRUE,
+ 'style_select' => TRUE,
+ ],
+ 'text_textarea_with_summary' => [
+ 'max_limit' => TRUE,
+ 'minimum_limit' => TRUE,
+ 'style_select' => TRUE,
+ ],
+ ];
+
+ return $settings[$plugin_id] ?? [];
+ }
+
+}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment