Focal Point Preview broken on forms altered with form_alter hooks

Created on 1 September 2022, over 2 years ago
Updated 4 October 2023, about 1 year ago

Problem/Motivation

I am attempting to programmatically create a title using First and Last name fields. It appears as though any attempt to use hook_form_alter(), hook_form_BASE_FORM_ID_alter(), hook_form_FORM_ID_alter() breaks the focal image preview on the media library, even if the module logic is something simple like 'print hello world' and does nothing to change the form.

My code solution for the custom module, which definitely breaks the focal point selector preview:


use Drupal\Core\Form\FormStateInterface;
use Drupal\node\NodeInterface;
 
/**
 * Implements hook_form_FORM_ID_alter().
 *
 * @param $form
 * @param \Drupal\Core\Form\FormStateInterface $form_state
 * @param $form_id
 */

 // Handles Person Page creation
function ucb_person_title_form_node_ucb_person_form_alter(&$form, FormStateInterface $form_state, $form_id) {
    $form['title']['#access'] = FALSE;
    $form['#entity_builders'][] = 'ucb_person_title_builder';
}

// Handles Person Page edit form
function ucb_person_title_form_node_ucb_person_edit_form_alter(&$form, FormStateInterface $form_state, $form_id){
  $form['title']['#access'] = FALSE;
  $form['#entity_builders'][] = 'ucb_person_title_builder';
}
 
/**
 * Title builder for Person Page & Person Page Edit Form.
 *
 * @param $entity_type
 * @param \Drupal\node\NodeInterface $node
 * @param $form
 * @param \Drupal\Core\Form\FormStateInterface $form_state
 */
function ucb_person_title_builder($entity_type, NodeInterface $node, $form, FormStateInterface $form_state) {
  $last = $node->get('field_ucb_person_last_name')->value;
  $first = $node->get('field_ucb_person_first_name')->value;

  // Remove whitespace
  $last = str_replace(' ', '', $last);
  $first = str_replace(' ', '', $first);
 
  // Set title
  $node->setTitle( $first . ' ' . $last);
}

Having any custom module that hits the form using any form_alter() hook seems to break the image preview (screenshot attached).

Steps to reproduce

- Have focal image module
- Create a simple module using hook_form_alter() or any other form_alter() hooks. You do not need to have any actual form edits or overrides, simply printing 'hello world' from an installed form_alter module seems to break the focal image preview in the media library.
- Enable both and upload an image to media library, results in no focal image preview

Proposed resolution

I'd like to get this working

Remaining tasks

- Why is this occuring?
- Can it be fixed?
- Will it be fixed?

User interface changes

n/a

API changes

n/a

Data model changes

n/a

πŸ› Bug report
Status

Closed: works as designed

Version

1.5

Component

Other Code

Created by

πŸ‡ΊπŸ‡ΈUnited States pabr5825

Live updates comments and jobs are added and updated live.
Sign in to follow issues

Comments & Activities

Not all content is available!

It's likely this issue predates Contrib.social: some issue and comment data are missing.

Production build 0.71.5 2024