Snippets

Daniel Sasser NCL Entity Ref Max Length Override

Created by Daniel Sasser
diff --git a/src/modules/oar_admin/oar_admin.module b/src/modules/oar_admin/oar_admin.module
index d222937a..9470cefa 100644
--- a/src/modules/oar_admin/oar_admin.module
+++ b/src/modules/oar_admin/oar_admin.module
@@ -6,6 +6,8 @@
  */

 use \Drupal\Core\Form\FormStateInterface;
+use \Drupal\Core\Field\WidgetInterface;
+use \Drupal\Core\Field\FieldDefinitionInterface;

 /**
  * Implements hook_form_FORM_ID_alter().
@@ -30,3 +32,43 @@ function oar_admin_page_attachments(array &$page) {
   }

 }
+
+/**
+ * Implements hook_field_widget_third_party_settings_form().
+ */
+function oar_admin_field_widget_third_party_settings_form(WidgetInterface $plugin, FieldDefinitionInterface $field_definition, $form_mode, $form, FormStateInterface $form_state) {
+  if ($plugin->getPluginId() === 'entity_reference_autocomplete_tags') {
+    $element['oar_admin_entity_ref_widget_max_length'] = array(
+      '#type' => 'number',
+      '#title' => t('Max Length'),
+      '#description' => t('The maximum number of characters allowed in the field.'),
+      '#default_value' => $plugin->getThirdPartySetting('oar_admin', 'oar_admin_entity_ref_widget_max_length') ?: '1024',
+    );
+    return $element;
+  }
+}
+
+/**
+ * Implements hook_field_widget_settings_summary_alter().
+ */
+function oar_admin_field_widget_settings_summary_alter(&$summary, $context) {
+  /** @var \Drupal\Core\Field\Plugin\Field\FieldWidget\EntityReferenceAutocompleteTagsWidget $widget */
+  $widget = $context['widget'];
+  if ($widget->getPluginId() === 'entity_reference_autocomplete_tags') {
+    if ($length = $widget->getThirdPartySetting('oar_admin', 'oar_admin_entity_ref_widget_max_length')) {
+      $summary[] = t('Max length: @length', ['@length' => $length]);
+    }
+  }
+}
+
+/**
+ * Implements hook_field_widget_WIDGET_TYPE_form_alter().
+ */
+function oar_admin_field_widget_entity_reference_autocomplete_tags_form_alter(&$element, FormStateInterface $form_state, $context) {
+  /** @var \Drupal\Core\Field\Plugin\Field\FieldWidget\EntityReferenceAutocompleteTagsWidget $widget */
+  $widget = $context['widget'];
+  $length = $widget->getThirdPartySetting('oar_admin', 'oar_admin_entity_ref_widget_max_length');
+  if ($length) {
+    $element['target_id']['#maxlength'] = $length;
+  }
+}

Comments (0)

HTTPS SSH

You can clone a snippet to your computer for local editing. Learn more.