You are here

public static function Dazzler::getFormIdSuggestion in Formdazzle! 2.x

Generate a more useful form ID suggestion.

Parameters

array $form: The form to examine.

string $form_id: The ID of the form.

Return value

string The generated $form_id_suggestion.

2 calls to Dazzler::getFormIdSuggestion()
Dazzler::preRenderForm in src/Dazzler.php
Adds template suggestions to forms.
DazzlerTest::testGetFormIdSuggestion in tests/src/Unit/DazzlerTest.php
@covers ::getFormIdSuggestion

File

src/Dazzler.php, line 130

Class

Dazzler
A class providing methods to modify Drupal form elements.

Namespace

Drupal\formdazzle

Code

public static function getFormIdSuggestion(array &$form, $form_id) {
  $form_id_suggestion = $form_id;
  $last_suggestion = '';
  if (isset($form['#theme'])) {
    if (is_string($form['#theme'])) {
      $last_suggestion = $form['#theme'];
    }
    else {
      $last_suggestion = $form['#theme'][array_key_last($form['#theme'])];
    }
  }

  // Use a simplified webform form ID.
  if ($last_suggestion === 'webform_submission_form' && isset($form['#webform_id'])) {
    $form_id_suggestion = 'webform_' . $form['#webform_id'];
  }
  elseif ($form_id === 'views_exposed_form') {

    // The first theme suggestion includes both the View name and display ID.
    // @see Drupal\views\ViewExecutable::buildThemeFunctions().
    $form_id_suggestion = str_replace('views_exposed_form__', 'views__', $form['#theme'][0]);
  }
  return $form_id_suggestion;
}