diff --git a/paragraphs.api.php b/paragraphs.api.php index 359b6ed..85d7c99 100644 --- a/paragraphs.api.php +++ b/paragraphs.api.php @@ -46,5 +46,116 @@ function hook_paragraphs_widget_actions_alter(array &$widget_actions, array &$co } /** + * Perform alterations on a paragraphs entity subform. + * + * Modules can implement hook_form_paragraphs_subform_alter() to change a + * paragraphs entity subform in the entity reference widgets. + * + * In addition to hook_form_paragraphs_subform_alter(), there are more specific + * form hooks available. This allows targeting of a specific widget type and/or + * paragraphs type form directly. Within each module, the alter hooks are + * called in the following order: + * - hook_form_paragraphs_subform_alter() + * - hook_form_paragraphs_subform_TYPE_alter() + * With TYPE being the paragraphs type of the paragraphs entity. + * - hook_form_paragraphs_subform_WIDGET_alter() + * With WIDGET being 'classic' or 'experimental'. + * - hook_form_paragraphs_subform_WIDGET_TYPE_alter() + * With WIDGET being 'classic' or 'experimental' and TYPE being the + * paragraphs type of the paragraphs entity. + * + * @param array $subform + * Nested array of form elements for the paragraphs entity subform in the + * widget. + * @param \Drupal\Core\Form\FormStateInterface $form_state + * The current state of the form. The arguments that + * \Drupal::formBuilder()->getForm() was originally called with are available + * in the array $form_state->getBuildInfo()['args']. + * @param int $delta + * The order of this item in the array of sub-elements (0, 1, 2, etc.). + * + * @see hook_form_paragraphs_subform_TYPE_alter() + * @see hook_form_paragraphs_subform_WIDGET_alter() + * @see hook_form_paragraphs_subform_WIDGET_TYPE_alter() + */ +function hook_form_paragraphs_subform_alter(array &$subform, \Drupal\Core\Form\FormStateInterface $form_state, $delta) { + $paragraph = $form_state->get('paragraph'); +} + +/** + * Perform alterations on a paragraphs entity subform. + * + * Modules can implement hook_form_paragraphs_subform_TYPE_alter() to change + * a paragraphs entity subform in the entity reference widgets for a specific + * paragraphs type. + * + * @param array $subform + * Nested array of form elements for the paragraphs entity subform in the + * widget. + * @param \Drupal\Core\Form\FormStateInterface $form_state + * The current state of the form. The arguments that + * \Drupal::formBuilder()->getForm() was originally called with are available + * in the array $form_state->getBuildInfo()['args']. + * @param int $delta + * The order of this item in the array of sub-elements (0, 1, 2, etc.). + * + * @see hook_form_paragraphs_subform_alter() + * @see hook_form_paragraphs_subform_WIDGET_alter() + * @see hook_form_paragraphs_subform_WIDGET_TYPE_alter() + */ +function hook_form_paragraphs_subform_TYPE_alter(array &$subform, \Drupal\Core\Form\FormStateInterface $form_state, $delta) { + $paragraph = $form_state->get('paragraph'); +} + +/** + * Perform alterations on a paragraphs entity subform. + * + * Modules can implement hook_form_paragraphs_subform_WIDGET_alter() to change + * a paragraphs entity subform in a specific entity reference widget. + * + * @param array $subform + * Nested array of form elements for the paragraphs entity subform in the + * widget. + * @param \Drupal\Core\Form\FormStateInterface $form_state + * The current state of the form. The arguments that + * \Drupal::formBuilder()->getForm() was originally called with are available + * in the array $form_state->getBuildInfo()['args']. + * @param int $delta + * The order of this item in the array of sub-elements (0, 1, 2, etc.). + * + * @see hook_form_paragraphs_subform_alter() + * @see hook_form_paragraphs_subform_TYPE_alter() + * @see hook_form_paragraphs_subform_WIDGET_TYPE_alter() + */ +function hook_form_paragraphs_subform_WIDGET_alter(array &$subform, \Drupal\Core\Form\FormStateInterface $form_state, $delta) { + $paragraph = $form_state->get('paragraph'); +} + +/** + * Perform alterations on a paragraphs entity subform. + * + * Modules can implement hook_form_paragraphs_subform_WIDGET_TYPE_alter() to + * change a paragraphs entity subform in a specific entity reference widget for + * and a specific paragraphs type. + * + * @param array $subform + * Nested array of form elements for the paragraphs entity subform in the + * widget. + * @param \Drupal\Core\Form\FormStateInterface $form_state + * The current state of the form. The arguments that + * \Drupal::formBuilder()->getForm() was originally called with are available + * in the array $form_state->getBuildInfo()['args']. + * @param int $delta + * The order of this item in the array of sub-elements (0, 1, 2, etc.). + * + * @see hook_form_paragraphs_subform_alter() + * @see hook_form_paragraphs_subform_TYPE_alter() + * @see hook_form_paragraphs_subform_WIDGET_alter() + */ +function hook_form_paragraphs_subform_WIDGET_TYPE_alter(array &$subform, \Drupal\Core\Form\FormStateInterface $form_state, $delta) { + $paragraph = $form_state->get('paragraph'); +} + +/** * @} End of "addtogroup hooks". */ diff --git a/src/Plugin/Field/FieldWidget/InlineParagraphsWidget.php b/src/Plugin/Field/FieldWidget/InlineParagraphsWidget.php index 36f681b..21b8c7e 100644 --- a/src/Plugin/Field/FieldWidget/InlineParagraphsWidget.php +++ b/src/Plugin/Field/FieldWidget/InlineParagraphsWidget.php @@ -664,6 +664,14 @@ class InlineParagraphsWidget extends WidgetBase { } } } + $form_state->set('paragraph', $paragraphs_entity); + $hooks = [ + 'form_paragraphs_subform', + 'form_paragraphs_subform_' . $paragraphs_entity->getParagraphType()->id(), + 'form_paragraphs_subform_classic', + 'form_paragraphs_subform_classic_' . $paragraphs_entity->getParagraphType()->id(), + ]; + \Drupal::ModuleHandler()->alter($hooks, $element['subform'], $form_state, $delta); } elseif ($item_mode == 'preview') { $element['subform'] = array(); diff --git a/src/Plugin/Field/FieldWidget/ParagraphsWidget.php b/src/Plugin/Field/FieldWidget/ParagraphsWidget.php index 84b47ea..f3651ac 100644 --- a/src/Plugin/Field/FieldWidget/ParagraphsWidget.php +++ b/src/Plugin/Field/FieldWidget/ParagraphsWidget.php @@ -732,6 +732,14 @@ class ParagraphsWidget extends WidgetBase { } } } + $form_state->set('paragraph', $paragraphs_entity); + $hooks = [ + 'form_paragraphs_subform', + 'form_paragraphs_subform_' . $paragraphs_entity->getParagraphType()->id(), + 'form_paragraphs_subform_experimental', + 'form_paragraphs_subform_experimental_' . $paragraphs_entity->getParagraphType()->id(), + ]; + \Drupal::ModuleHandler()->alter($hooks, $element['subform'], $form_state, $delta); } elseif ($item_mode == 'closed') { $element['subform'] = [];