Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Replace field_widget_form_alter hook that was removed from core #355

Merged
merged 2 commits into from
Dec 20, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 0 additions & 36 deletions modules/stanford_news/stanford_news.module
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,6 @@
use Drupal\views\ViewExecutable;
use Drupal\node\NodeInterface;
use Drupal\views\Plugin\views\cache\CachePluginBase;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Render\Element;
use Drupal\field\FieldConfigInterface;

/**
* Implements hook_theme().
Expand Down Expand Up @@ -97,36 +94,3 @@ function stanford_news_views_post_render(ViewExecutable $view, &$output, CachePl
}
}
}

/**
* Implements hook_field_widget_form_alter().
*/
function stanford_news_field_widget_form_alter(&$element, FormStateInterface $form_state, $context) {
$field = $context['items']->getFieldDefinition();
if ($field instanceof FieldConfigInterface && $field->id() == "node.stanford_news.su_news_topics") {
$element['#stanford_news_topics_add_more'] = t('Add another term');
}
}

/**
* Implements hook_preprocess_field_multiple_value_form().
*
* We look for a value that was placed there earlier by
* stanford_news_field_widget_form_alter() and change the add_more button
* to use that.
*/
function stanford_news_preprocess_field_multiple_value_form(&$variables) {
foreach (Element::children($variables['element']) as $child) {
$child_element = &$variables['element'][$child];
if (isset($child_element['#stanford_news_topics_add_more'])) {

if (isset($variables['element']['add_more']['#value']) && $variables['element']['add_more']['#value'] != $child_element['#stanford_news_topics_add_more']) {
$variables['element']['add_more']['#value'] = $child_element['#stanford_news_topics_add_more'];
}

if (isset($variables['button']['#value']) && $variables['button']['#value'] != $child_element['#stanford_news_topics_add_more']) {
$variables['button']['#value'] = $child_element['#stanford_news_topics_add_more'];
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -28,13 +28,13 @@ function stanford_person_importer_help($route_name, RouteMatchInterface $route_m
}

/**
* Implements hook_field_widget_form_alter().
* Implements hook_field_widget_complete_form_alter().
*/
function stanford_person_importer_field_widget_form_alter(&$element, FormStateInterface $form_state, $context) {
function stanford_person_importer_field_widget_complete_form_alter(&$field_widget_complete_form, FormStateInterface $form_state, $context) {
$field_name = $context['items']->getFieldDefinition()->getName();
if ($field_name == 'su_person_cap_password') {
// Validate the credentials on the config pages entity form.
$element['#element_validate'][] = [
$field_widget_complete_form['widget'][0]['#element_validate'][] = [
'Drupal\stanford_person_importer\Cap',
'validateCredentials',
];
Expand Down
12 changes: 6 additions & 6 deletions modules/stanford_profile_styles/stanford_profile_styles.module
Original file line number Diff line number Diff line change
Expand Up @@ -103,17 +103,17 @@ function stanford_profile_styles_library_info_alter(&$libraries, $extension) {
}

/**
* Implements hook_field_widget_multivalue_WIDGET_TYPE_form_alter().
* Implements hook_field_widget_complete_WIDGET_TYPE_form_alter().
*/
function stanford_profile_styles_field_widget_multivalue_paragraphs_form_alter(array &$elements, FormStateInterface $form_state, array $context) {
$elements['#attached']['library'][] = 'stanford_profile_styles/admin.field_widgets';
function stanford_profile_styles_field_widget_complete_paragraphs_form_alter(&$field_widget_complete_form, FormStateInterface $form_state, $context) {
$field_widget_complete_form['#attached']['library'][] = 'stanford_profile_styles/admin.field_widgets';
}

/**
* Implements hook_field_widget_WIDGET_TYPE_form_alter().
* Implements hook_field_widget_complete_WIDGET_TYPE_form_alter().
*/
function stanford_profile_styles_field_widget_smartdate_timezone_form_alter(array &$elements, FormStateInterface $form_state, array $context) {
$elements['#attached']['library'][] = 'stanford_profile_styles/admin.field_widgets';
function stanford_profile_styles_field_widget_complete_smartdate_timezone_form_alter(&$field_widget_complete_form, FormStateInterface $form_state, $context) {
$field_widget_complete_form['#attached']['library'][] = 'stanford_profile_styles/admin.field_widgets';
}

/**
Expand Down
20 changes: 10 additions & 10 deletions modules/stanford_publication/stanford_publication.module
Original file line number Diff line number Diff line change
Expand Up @@ -224,9 +224,9 @@ function stanford_publication_node_delete(NodeInterface $entity) {
}

/**
* Implements hook_field_widget_form_alter().
* Implements hook_field_widget_complete_form_alter().
*/
function stanford_publication_field_widget_form_alter(&$element, FormStateInterface $form_state, $context) {
function stanford_publication_field_widget_complete_form_alter(&$field_widget_complete_form, FormStateInterface $form_state, $context) {
if (!isset($context['items']) || !($context['items'] instanceof FieldItemListInterface)) {
return;
}
Expand All @@ -236,26 +236,26 @@ function stanford_publication_field_widget_form_alter(&$element, FormStateInterf
$context['items']->getName() == 'title' &&
$context['items']->getEntity()->getEntityTypeId() == 'citation'
) {
$element['value']['#description'] = t('The title of the Publication');
$field_widget_complete_form['widget'][0]['value']['#description'] = t('The title of the Publication');
}

if ($context['items']->getName() == 'su_publication_citation') {

// Tweak the "Add New" button on the inline entity form.
if (!empty($element['actions']['ief_add'])) {
if (!empty($field_widget_complete_form['widget']['actions']['ief_add'])) {
/** @var \Drupal\Core\StringTranslation\TranslatableMarkup $button_value */
$button_value = $element['actions']['ief_add']['#value'];
$element['actions']['ief_add']['#value'] = t('Add @type_singular', $button_value->getArguments(), $button_value->getOptions());
$button_value = $field_widget_complete_form['widget']['actions']['ief_add']['#value'];
$field_widget_complete_form['widget']['actions']['ief_add']['#value'] = t('Add @type_singular', $button_value->getArguments(), $button_value->getOptions());
}

// Add the citation bundle name to the top for quick reference.
if (!empty($element['form']['inline_entity_form'])) {
$entity_type = $element['form']['inline_entity_form']['#entity_type'];
$bundle = $element['form']['inline_entity_form']['#bundle'];
if (!empty($field_widget_complete_form['widget']['form']['inline_entity_form'])) {
$entity_type = $field_widget_complete_form['widget']['form']['inline_entity_form']['#entity_type'];
$bundle = $field_widget_complete_form['widget']['form']['inline_entity_form']['#bundle'];
/** @var \Drupal\Core\Entity\EntityTypeBundleInfoInterface $bundle_service */
$bundle_service = \Drupal::service('entity_type.bundle.info');
$bundle_name = $bundle_service->getBundleInfo($entity_type)[$bundle]['label'];
$element['form']['inline_entity_form']['#prefix'] = "$bundle_name - {$element['#title']}";
$field_widget_complete_form['widget']['form']['inline_entity_form']['#prefix'] = "$bundle_name - {$field_widget_complete_form['widget']['#title']}";
}
}
}
Expand Down
32 changes: 14 additions & 18 deletions stanford_profile_helper.module
Original file line number Diff line number Diff line change
Expand Up @@ -502,12 +502,13 @@ function stanford_profile_helper_entity_field_access($operation, FieldDefinition
}

/**
* Implements hook_field_widget_WIDGET_TYPE_form_alter().
* Implements hook_field_widget_complete_WIDGET_TYPE_form_alter().
*/
function stanford_profile_helper_field_widget_options_select_form_alter(&$element, FormStateInterface $form_state, $context) {
if ($context['items']->getFieldDefinition()
->getName() == 'layout_selection') {
$element['#description'] = t('Choose a layout to display the page as a whole. Choose "- None -" to keep the default layout setting.');
function stanford_profile_helper_field_widget_complete_options_select_form_alter(&$field_widget_complete_form, FormStateInterface $form_state, $context) {
/** @var \Drupal\Core\Field\FieldStorageDefinitionInterface $field_def */
$field_def = $context['items']->getFieldDefinition();
if ($field_def->getName() == 'layout_selection') {
$field_widget_complete_form['widget']['#description'] = t('Choose a layout to display the page as a whole. Choose "- None -" to keep the default layout setting.');
}
}

Expand Down Expand Up @@ -935,35 +936,30 @@ function stanford_profile_helper_preprocess_menu(&$variables) {
}

/**
* Implements hook_field_widget_form_alter().
* Implements hook_field_widget_complete_form_alter().
*/
function stanford_profile_helper_field_widget_form_alter(&$element, FormStateInterface $form_state, $context) {
if ($context['items']->getName() == 'su_page_components') {
// Push pages to only allow 3 items per row but don't break any existing
// pages that have 4 per row.
$element['container']['value']['#attached']['drupalSettings']['reactParagraphs'][0]['itemsPerRow'] = 3;
}

function stanford_profile_helper_field_widget_complete_form_alter(&$field_widget_complete_form, FormStateInterface $form_state, $context) {
if ($context['items']->getName() == 'field_media_embeddable_oembed') {
$user = \Drupal::currentUser();
$authorized = $user->hasPermission('create field_media_embeddable_code')
|| $user->hasPermission('edit field_media_embeddable_code');

if (!$authorized) {
$args = $element['value']['#description']['#items'][1]->getArguments();
$args = $field_widget_complete_form['widget'][0]['value']['#description']['#items'][1]->getArguments();
$args['@snow_form'] = 'https://stanford.service-now.com/it_services?id=sc_cat_item&sys_id=83daed294f4143009a9a97411310c70a';
$new_desc = 'Allowed providers: @providers. For custom embeds, please <a href="@snow_form">request support.</a>';
$element['value']['#description'] = t($new_desc, $args);
$field_widget_complete_form['widget'][0]['value']['#description'] = t($new_desc, $args);
}
}
}

/**
* Implements hook_field_widget_WIDGET_TYPE_form_alter().
* Implements hook_field_widget_complete_WIDGET_TYPE_form_alter().
*/
function stanford_profile_helper_field_widget_datetime_timestamp_no_default_form_alter(&$element, FormStateInterface $form_state, $context) {
function stanford_profile_helper_field_widget_complete_datetime_timestamp_no_default_form_alter(&$field_widget_complete_form, FormStateInterface $form_state, $context) {
// Set the date increment for scheduler settings.
$state = \Drupal::state();
$element['value']['#date_increment'] = $state->get('stanford_profile_helper.scheduler_increment', 60 * 60 * 4);
$field_widget_complete_form['widget'][0]['value']['#date_increment'] = $state->get('stanford_profile_helper.scheduler_increment', 60 * 60 * 4);
}

/**
Expand Down
Loading