pluginformalter-8.x-1.x-dev/pluginformalter.module
pluginformalter.module
<?php
/**
* @file
* Contains pluginformalter.module.
*/
use Drupal\Core\Form\FormState;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Routing\RouteMatchInterface;
/**
* Implements hook_help().
*/
function pluginformalter_help($route_name, RouteMatchInterface $route_match) {
switch ($route_name) {
// Main module help for the plugin_formalter module.
case 'help.page.pluginformalter':
$output = '';
$output .= '<h3>' . t('About') . '</h3>';
$output .= '<p>' . t('Provides an annotation Plugin to be used as replacement of hook_form_alter().') . '</p>';
return $output;
default:
}
}
/**
* Implements hook_form_alter().
*/
function pluginformalter_form_alter(&$form, FormStateInterface &$form_state, $form_id) {
// Form alter hooks are called in the following order: hook_form_alter(),
// hook_form_BASE_FORM_ID_alter(), hook_form_FORM_ID_alter().
// Since we are gonna alter forms by using both base_form_id and form_id, we
// have to keep the altering order untouched. base_form_id comes first,
// form_id follows.
$build_info = $form_state->getBuildInfo();
if (isset($build_info['base_form_id'])) {
pluginformalter_alter_form(['base_form_id' => $build_info['base_form_id']], $form, $form_state, $form_id);
}
pluginformalter_alter_form(['form_id' => $form_id], $form, $form_state, $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);
}
/**
* Implements hook_field_widget_single_element_WIDGET_TYPE_form_alter().
*/
function pluginformalter_field_widget_single_element_paragraphs_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 \Drupal\Core\Form\FormStateInterface $form_state
* The form state instance.
*/
function pluginformalter_alter_paragraphs(array &$element, FormStateInterface &$form_state) {
if (empty($element['#paragraph_type'])) {
return;
}
$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.
* @param array $form
* The Form build array.
* @param \Drupal\Core\Form\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_form(array $options, array &$form, FormStateInterface &$form_state, $form_id) {
/** @var \Drupal\pluginformalter\Plugin\FormAlterManager $pluginManager */
$pluginManager = \Drupal::service('plugin.manager.form_alter');
$pluginManager->clearCachedDefinitions();
/** @var \Drupal\pluginformalter\Plugin\FormAlterInterface $plugin */
foreach ($pluginManager->getInstance($options) as $plugin) {
if (version_compare(\Drupal::VERSION, '11.2', '>=')) {
@trigger_error('"FormAlter as a Plugin" module is deprecated. Your plugin "' . $plugin->getPluginId() . '" will stop being called in Drupal 12.0.0. Use OOP Hooks instead. See https://www.drupal.org/project/pluginformalter/issues/3549687', E_USER_DEPRECATED);
}
$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 \Drupal\Core\Form\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');
$pluginManager->clearCachedDefinitions();
/** @var \Drupal\pluginformalter\Plugin\FormAlterInterface $plugin */
foreach ($pluginManager->getInstance($options) as $plugin) {
if (version_compare(\Drupal::VERSION, '11.2', '>=')) {
@trigger_error('"FormAlter as a Plugin" module is deprecated. Your plugin "' . $plugin->getPluginId() . '" will stop being called in Drupal 12.0.0. Use OOP Hooks instead. See https://www.drupal.org/project/pluginformalter/issues/3549687', E_USER_DEPRECATED);
}
$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 \Drupal\Core\Form\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, FormStateInterface &$form_state, $form_id) {
if (empty($form)) {
return;
}
/** @var \Drupal\pluginformalter\Plugin\InlineEntityFormAlterManager $pluginManager */
$pluginManager = \Drupal::service('plugin.manager.form_alter.ief');
$pluginManager->clearCachedDefinitions();
/** @var \Drupal\pluginformalter\Plugin\FormAlterInterface $plugin */
foreach ($pluginManager->getInstance($options) as $plugin) {
if (version_compare(\Drupal::VERSION, '11.2', '>=')) {
@trigger_error('"FormAlter as a Plugin" module is deprecated. Your plugin "' . $plugin->getPluginId() . '" will stop being called in Drupal 12.0.0. Use OOP Hooks instead. See https://www.drupal.org/project/pluginformalter/issues/3549687', E_USER_DEPRECATED);
}
$plugin->formAlter($form, $form_state, $form_id);
}
}
