If we have a form element on an entity that has a JS state condition that is looking for the name field, as soon as the field replacement is enabled, the state condition fails since the title element has changed from something like 'title' to 'title[en][0][value]'.

Comments

Dave Reid’s picture

Here's some code I put into test in title.module. I'm not sure it would be acceptable. And it only works for conditions that use the 'name' selector search.

/**
 * Fix any state conditions to use the new title field's name.
 */
function _title_field_process_states(&$form, $legacy_field_name, $replacement_field_name, $langcode) {
  foreach (element_children($form) as $key) {
    if (!empty($form[$key]['#states'])) {
      foreach ($form[$key]['#states'] as $state => &$conditions) {
        foreach ($conditions as $selector => $condition) {
          if (is_string($selector) && strpos($selector, '[name="' . $legacy_field_name . '"]')) {
            $replacement_field_actual_name = "{$legacy_field_name}[{$langcode}][0][value]";
            $new_selector = str_replace('[name="' . $legacy_field_name . '"]', '[name="' . $replacement_field_actual_name . '"]', $selector);
            $conditions[$new_selector] = $condition;
            unset($conditions[$selector]);
          }
        }
      }
    }

    // Recurse into nested form fields.
    _title_field_process_states($form[$key], $legacy_field_name, $replacement_field_name, $langcode);
  }
}