Loading pluginformalter.module +136 −2 Original line number Diff line number Diff line Loading @@ -5,6 +5,7 @@ * Contains pluginformalter.module. */ use Drupal\Core\Form\FormState; use Drupal\Core\Form\FormStateInterface; use Drupal\Core\Routing\RouteMatchInterface; Loading Loading @@ -41,8 +42,55 @@ function pluginformalter_form_alter(&$form, FormStateInterface &$form_state, $fo } /** * Alter forms by using Plugin Form Alter plugins fetched by base_form_id or * form_id. * Paragraphs support. */ /** * Implements hook_field_widget_paragraphs_form_alter(). */ function pluginformalter_field_widget_paragraphs_form_alter(array &$element, FormStateInterface &$form_state, array $context) { pluginformalter_alter_paragraphs($element, $form_state); } /** * Implements hook_field_widget_entity_reference_paragraphs_form_alter(). */ function pluginformalter_field_widget_entity_reference_paragraphs_form_alter(array &$element, FormStateInterface &$form_state, array $context) { pluginformalter_alter_paragraphs($element, $form_state); } /** * Implements hook_field_widget_entity_reference_paragraphs_browser_form_alter(). */ function pluginformalter_field_widget_entity_reference_paragraphs_browser_form_alter(array &$element, FormStateInterface &$form_state, array $context) { pluginformalter_alter_paragraphs($element, $form_state); } /** * Implements hook_field_widget_paragraphs_browser_form_alter(). */ function pluginformalter_field_widget_paragraphs_browser_form_alter(array &$element, FormStateInterface &$form_state, array $context) { pluginformalter_alter_paragraphs($element, $form_state); } /** * Utility function used to alter paragraphs forms. * * @param array $element * The form element. * @param FormStateInterface $form_state * The form state instance. */ function pluginformalter_alter_paragraphs(array &$element, FormStateInterface &$form_state) { $options = [ 'paragraph_type' => $element['#paragraph_type'], ]; $form_id = $form_state->getBuildInfo()['form_id']; pluginformalter_alter_paragraphs_form($options, $element, $form_state, $form_id); } /** * Alter forms by using Form Alter plugins fetched by base_form_id or form_id. * * @param array $options * An array of options used to fetch the plugin. Loading @@ -61,3 +109,89 @@ function pluginformalter_alter_form(array $options, array &$form, FormStateInter $plugin->formAlter($form, $form_state, $form_id); } } /** * Alter Paragraphs forms by using Paragraphs Form Alter plugins fetched * by paragraph_type. * * @param array $options * An array of options used to fetch the plugin. * @param array $form * The Form build array. * @param FormStateInterface $form_state * The Form State instance. * @param string $form_id * The form id of the form which is gonna be altered. */ function pluginformalter_alter_paragraphs_form(array $options, array &$form, FormStateInterface &$form_state, $form_id) { /** @var \Drupal\pluginformalter\Plugin\ParagraphsFormAlterManager $pluginManager */ $pluginManager = \Drupal::service('plugin.manager.form_alter.paragraphs'); /** @var \Drupal\pluginformalter\Plugin\FormAlterInterface $plugin */ foreach ($pluginManager->getInstance($options) as $plugin) { $plugin->formAlter($form, $form_state, $form_id); } } /** * Inline Entity Form support. */ /** * Implements hook_inline_entity_form_entity_form_alter(). */ function pluginformalter_inline_entity_form_entity_form_alter(&$entity_form, &$form_state) { $options = [ 'type' => 'entity_form', 'entity_type' => $entity_form['#entity_type'], 'bundle' => $entity_form['#bundle'], ]; $form_id = $form_state->getBuildInfo()['form_id']; pluginformalter_alter_inline_entity_form($options, $entity_form, $form_state, $form_id); } /** * Implements hook_inline_entity_form_reference_form_alter(). */ function pluginformalter_inline_entity_form_reference_form_alter(&$reference_form, &$form_state) { $options = [ 'type' => 'reference_form', 'entity_type' => $reference_form['#entity_type'], ]; $form_id = $form_state->getBuildInfo()['form_id']; pluginformalter_alter_inline_entity_form($options, $reference_form, $form_state, $form_id); } /** * Implements hook_inline_entity_form_table_fields_alter(). */ function pluginformalter_inline_entity_form_table_fields_alter(&$fields, $context) { $context['type'] = 'table_fields'; $form_state = new FormState(); $form_id = 'inline_entity_form_table_fields'; pluginformalter_alter_inline_entity_form($context, $fields, $form_state, $form_id); } /** * Alter Inline Entity Forms by using Inline Entity Form Alter plugins fetched * by paragraph_type. * * @param array $options * An array of options used to fetch the plugin. * @param array|null $form * The Form build array. * @param FormStateInterface $form_state * The Form State instance. * @param string $form_id * The form id of the form which is gonna be altered. */ function pluginformalter_alter_inline_entity_form(array $options, array &$form = NULL, FormStateInterface &$form_state, $form_id) { if (empty($form)) { return; } /** @var \Drupal\pluginformalter\Plugin\InlineEntityFormAlterManager $pluginManager */ $pluginManager = \Drupal::service('plugin.manager.form_alter.ief'); /** @var \Drupal\pluginformalter\Plugin\FormAlterInterface $plugin */ foreach ($pluginManager->getInstance($options) as $plugin) { $plugin->formAlter($form, $form_state, $form_id); } } pluginformalter.services.yml +8 −0 Original line number Diff line number Diff line Loading @@ -2,3 +2,11 @@ services: plugin.manager.form_alter: class: Drupal\pluginformalter\Plugin\FormAlterManager parent: default_plugin_manager plugin.manager.form_alter.paragraphs: class: Drupal\pluginformalter\Plugin\ParagraphsFormAlterManager parent: default_plugin_manager plugin.manager.form_alter.ief: class: Drupal\pluginformalter\Plugin\InlineEntityFormAlterManager parent: default_plugin_manager src/Annotation/InlineEntityFormAlter.php 0 → 100644 +93 −0 Original line number Diff line number Diff line <?php namespace Drupal\pluginformalter\Annotation; use Drupal\Component\Annotation\Plugin; /** * Defines a Inline Entity Form alter item annotation object. * * @see \Drupal\pluginformalter\Plugin\InlineEntityFormAlterManager * @see plugin_api * * @Annotation */ class InlineEntityFormAlter extends Plugin { /** * The plugin ID. * * @var string */ public $id; /** * The label of the plugin. * * @var \Drupal\Core\Annotation\Translation * * @ingroup plugin_translatable */ public $label; /** * The type of form we are going to alter. * Allowed values are the following: * - entity_form * - reference_form * - table_fields * * @var string */ public $type; /** * The type of the referenced entities. * * @var string */ public $entity_type; /** * The bundle of the referenced entity. * * @var string */ public $bundle; /** * The name of the reference field on which IEF is operating. * * @var string */ public $field_name; /** * Bundles allowed on the reference field. * * @var string */ public $allowed_bundles; /** * The type of the parent entity. * * @var string */ public $parent_entity_type; /** * The bundle of the parent entity. * * @var string */ public $parent_bundle; /** * The plugin weight which affects the alterations queue. * * @var int */ public $weight = 0; } src/Annotation/ParagraphsFormAlter.php 0 → 100644 +47 −0 Original line number Diff line number Diff line <?php namespace Drupal\pluginformalter\Annotation; use Drupal\Component\Annotation\Plugin; /** * Defines a Paragraphs Form alter item annotation object. * * @see \Drupal\pluginformalter\Plugin\ParagraphsFormAlterManager * @see plugin_api * * @Annotation */ class ParagraphsFormAlter extends Plugin { /** * The plugin ID. * * @var string */ public $id; /** * The label of the plugin. * * @var \Drupal\Core\Annotation\Translation * * @ingroup plugin_translatable */ public $label; /** * An array of paragraph types to be altered. * * @var array */ public $paragraph_type; /** * The plugin weight which affects the alterations queue. * * @var int */ public $weight = 0; } src/Plugin/FormAlterManager.php +2 −20 Original line number Diff line number Diff line Loading @@ -12,6 +12,8 @@ use Drupal\Core\Plugin\DefaultPluginManager; */ class FormAlterManager extends DefaultPluginManager { use FormAlterManagerTrait; /** * Constructs a new FormAlterManager object. * Loading Loading @@ -48,24 +50,4 @@ class FormAlterManager extends DefaultPluginManager { return $plugins; } /** * Sort plugins by weight. * * @param \Drupal\Component\Plugin\PluginInspectionInterface $a * A Form Alter plugin. * @param \Drupal\Component\Plugin\PluginInspectionInterface $b * A Form Alter plugin. * * @return int * The sorting result. */ protected function sort(PluginInspectionInterface $a, PluginInspectionInterface $b) { $a_definition = $a->getPluginDefinition(); $b_definition = $b->getPluginDefinition(); if ($a_definition['weight'] == $b_definition['weight']) { return 0; } return ($a_definition['weight'] < $b_definition['weight']) ? -1 : 1; } } Loading
pluginformalter.module +136 −2 Original line number Diff line number Diff line Loading @@ -5,6 +5,7 @@ * Contains pluginformalter.module. */ use Drupal\Core\Form\FormState; use Drupal\Core\Form\FormStateInterface; use Drupal\Core\Routing\RouteMatchInterface; Loading Loading @@ -41,8 +42,55 @@ function pluginformalter_form_alter(&$form, FormStateInterface &$form_state, $fo } /** * Alter forms by using Plugin Form Alter plugins fetched by base_form_id or * form_id. * Paragraphs support. */ /** * Implements hook_field_widget_paragraphs_form_alter(). */ function pluginformalter_field_widget_paragraphs_form_alter(array &$element, FormStateInterface &$form_state, array $context) { pluginformalter_alter_paragraphs($element, $form_state); } /** * Implements hook_field_widget_entity_reference_paragraphs_form_alter(). */ function pluginformalter_field_widget_entity_reference_paragraphs_form_alter(array &$element, FormStateInterface &$form_state, array $context) { pluginformalter_alter_paragraphs($element, $form_state); } /** * Implements hook_field_widget_entity_reference_paragraphs_browser_form_alter(). */ function pluginformalter_field_widget_entity_reference_paragraphs_browser_form_alter(array &$element, FormStateInterface &$form_state, array $context) { pluginformalter_alter_paragraphs($element, $form_state); } /** * Implements hook_field_widget_paragraphs_browser_form_alter(). */ function pluginformalter_field_widget_paragraphs_browser_form_alter(array &$element, FormStateInterface &$form_state, array $context) { pluginformalter_alter_paragraphs($element, $form_state); } /** * Utility function used to alter paragraphs forms. * * @param array $element * The form element. * @param FormStateInterface $form_state * The form state instance. */ function pluginformalter_alter_paragraphs(array &$element, FormStateInterface &$form_state) { $options = [ 'paragraph_type' => $element['#paragraph_type'], ]; $form_id = $form_state->getBuildInfo()['form_id']; pluginformalter_alter_paragraphs_form($options, $element, $form_state, $form_id); } /** * Alter forms by using Form Alter plugins fetched by base_form_id or form_id. * * @param array $options * An array of options used to fetch the plugin. Loading @@ -61,3 +109,89 @@ function pluginformalter_alter_form(array $options, array &$form, FormStateInter $plugin->formAlter($form, $form_state, $form_id); } } /** * Alter Paragraphs forms by using Paragraphs Form Alter plugins fetched * by paragraph_type. * * @param array $options * An array of options used to fetch the plugin. * @param array $form * The Form build array. * @param FormStateInterface $form_state * The Form State instance. * @param string $form_id * The form id of the form which is gonna be altered. */ function pluginformalter_alter_paragraphs_form(array $options, array &$form, FormStateInterface &$form_state, $form_id) { /** @var \Drupal\pluginformalter\Plugin\ParagraphsFormAlterManager $pluginManager */ $pluginManager = \Drupal::service('plugin.manager.form_alter.paragraphs'); /** @var \Drupal\pluginformalter\Plugin\FormAlterInterface $plugin */ foreach ($pluginManager->getInstance($options) as $plugin) { $plugin->formAlter($form, $form_state, $form_id); } } /** * Inline Entity Form support. */ /** * Implements hook_inline_entity_form_entity_form_alter(). */ function pluginformalter_inline_entity_form_entity_form_alter(&$entity_form, &$form_state) { $options = [ 'type' => 'entity_form', 'entity_type' => $entity_form['#entity_type'], 'bundle' => $entity_form['#bundle'], ]; $form_id = $form_state->getBuildInfo()['form_id']; pluginformalter_alter_inline_entity_form($options, $entity_form, $form_state, $form_id); } /** * Implements hook_inline_entity_form_reference_form_alter(). */ function pluginformalter_inline_entity_form_reference_form_alter(&$reference_form, &$form_state) { $options = [ 'type' => 'reference_form', 'entity_type' => $reference_form['#entity_type'], ]; $form_id = $form_state->getBuildInfo()['form_id']; pluginformalter_alter_inline_entity_form($options, $reference_form, $form_state, $form_id); } /** * Implements hook_inline_entity_form_table_fields_alter(). */ function pluginformalter_inline_entity_form_table_fields_alter(&$fields, $context) { $context['type'] = 'table_fields'; $form_state = new FormState(); $form_id = 'inline_entity_form_table_fields'; pluginformalter_alter_inline_entity_form($context, $fields, $form_state, $form_id); } /** * Alter Inline Entity Forms by using Inline Entity Form Alter plugins fetched * by paragraph_type. * * @param array $options * An array of options used to fetch the plugin. * @param array|null $form * The Form build array. * @param FormStateInterface $form_state * The Form State instance. * @param string $form_id * The form id of the form which is gonna be altered. */ function pluginformalter_alter_inline_entity_form(array $options, array &$form = NULL, FormStateInterface &$form_state, $form_id) { if (empty($form)) { return; } /** @var \Drupal\pluginformalter\Plugin\InlineEntityFormAlterManager $pluginManager */ $pluginManager = \Drupal::service('plugin.manager.form_alter.ief'); /** @var \Drupal\pluginformalter\Plugin\FormAlterInterface $plugin */ foreach ($pluginManager->getInstance($options) as $plugin) { $plugin->formAlter($form, $form_state, $form_id); } }
pluginformalter.services.yml +8 −0 Original line number Diff line number Diff line Loading @@ -2,3 +2,11 @@ services: plugin.manager.form_alter: class: Drupal\pluginformalter\Plugin\FormAlterManager parent: default_plugin_manager plugin.manager.form_alter.paragraphs: class: Drupal\pluginformalter\Plugin\ParagraphsFormAlterManager parent: default_plugin_manager plugin.manager.form_alter.ief: class: Drupal\pluginformalter\Plugin\InlineEntityFormAlterManager parent: default_plugin_manager
src/Annotation/InlineEntityFormAlter.php 0 → 100644 +93 −0 Original line number Diff line number Diff line <?php namespace Drupal\pluginformalter\Annotation; use Drupal\Component\Annotation\Plugin; /** * Defines a Inline Entity Form alter item annotation object. * * @see \Drupal\pluginformalter\Plugin\InlineEntityFormAlterManager * @see plugin_api * * @Annotation */ class InlineEntityFormAlter extends Plugin { /** * The plugin ID. * * @var string */ public $id; /** * The label of the plugin. * * @var \Drupal\Core\Annotation\Translation * * @ingroup plugin_translatable */ public $label; /** * The type of form we are going to alter. * Allowed values are the following: * - entity_form * - reference_form * - table_fields * * @var string */ public $type; /** * The type of the referenced entities. * * @var string */ public $entity_type; /** * The bundle of the referenced entity. * * @var string */ public $bundle; /** * The name of the reference field on which IEF is operating. * * @var string */ public $field_name; /** * Bundles allowed on the reference field. * * @var string */ public $allowed_bundles; /** * The type of the parent entity. * * @var string */ public $parent_entity_type; /** * The bundle of the parent entity. * * @var string */ public $parent_bundle; /** * The plugin weight which affects the alterations queue. * * @var int */ public $weight = 0; }
src/Annotation/ParagraphsFormAlter.php 0 → 100644 +47 −0 Original line number Diff line number Diff line <?php namespace Drupal\pluginformalter\Annotation; use Drupal\Component\Annotation\Plugin; /** * Defines a Paragraphs Form alter item annotation object. * * @see \Drupal\pluginformalter\Plugin\ParagraphsFormAlterManager * @see plugin_api * * @Annotation */ class ParagraphsFormAlter extends Plugin { /** * The plugin ID. * * @var string */ public $id; /** * The label of the plugin. * * @var \Drupal\Core\Annotation\Translation * * @ingroup plugin_translatable */ public $label; /** * An array of paragraph types to be altered. * * @var array */ public $paragraph_type; /** * The plugin weight which affects the alterations queue. * * @var int */ public $weight = 0; }
src/Plugin/FormAlterManager.php +2 −20 Original line number Diff line number Diff line Loading @@ -12,6 +12,8 @@ use Drupal\Core\Plugin\DefaultPluginManager; */ class FormAlterManager extends DefaultPluginManager { use FormAlterManagerTrait; /** * Constructs a new FormAlterManager object. * Loading Loading @@ -48,24 +50,4 @@ class FormAlterManager extends DefaultPluginManager { return $plugins; } /** * Sort plugins by weight. * * @param \Drupal\Component\Plugin\PluginInspectionInterface $a * A Form Alter plugin. * @param \Drupal\Component\Plugin\PluginInspectionInterface $b * A Form Alter plugin. * * @return int * The sorting result. */ protected function sort(PluginInspectionInterface $a, PluginInspectionInterface $b) { $a_definition = $a->getPluginDefinition(); $b_definition = $b->getPluginDefinition(); if ($a_definition['weight'] == $b_definition['weight']) { return 0; } return ($a_definition['weight'] < $b_definition['weight']) ? -1 : 1; } }