Skip to content
Snippets Groups Projects
Commit bbe3e149 authored by James Wilson's avatar James Wilson
Browse files

Issue #3450149 by jwilson3: OOM issue when debugging is enabled

parent 376c641d
No related branches found
No related tags found
1 merge request!11Issue #3450149 by jwilson3: Fix OOM issue with label_help_debug
Pipeline #183505 passed
......@@ -31,11 +31,28 @@ This module requires no modules outside of Drupal core.
## Troubleshooting
If a given field or widget type is not behaving like you would expect, you can
enable debug mode in settings.php with the following line:
1. Label Help placement logic 'use case' debugging.
$settings['label_help_debug'] = TRUE;
If a given field or widget type is not behaving as expected, enable debug
mode in settings.php with the following line:
$settings['label_help_debug'] = TRUE;
Then reload the form page to see debug output identifying the field widget
type and use case matched by Label Help placement logic. If no use case is
detected you'll see a warning message.
2. Form API dump debugging
To troubleshoot Form API structures for a given form field enable the Form
API element dump mode in settings.php with the following line:
$settings['label_help_debug_dump'] = TRUE;
This debug mode leverages the Symfony Variable Dumper `dump()` to provide
a drillable screen dump of the Form API element array. However, because
this dump runs inside the form_alter, it can result in incomplete page
load. This is expected.
## Using Label Help to create form fields programmatically
......
......@@ -66,11 +66,15 @@ function label_help_form_alter(&$form, &$form_state, $form_id) {
return;
}
$debug = Settings::get('label_help_debug', FALSE);
$debug_dump = Settings::get('label_help_debug_dump', FALSE);
$one_time_debug_warning = &drupal_static(__FUNCTION__);
foreach ($children as $key => $item) {
$occurrence = 0;
$use_case = 0;
$field = $form_object->getEntity()->getFieldDefinition($key);
$content = NULL;
$fallback_use_case = FALSE;
if ($field && method_exists($field, 'getThirdPartySetting')) {
$content = $field->getThirdPartySetting('label_help', 'label_help_description');
}
......@@ -92,13 +96,13 @@ function label_help_form_alter(&$form, &$form_state, $form_id) {
// appears in the table header, instead of inside the draggable row.
if (!empty($item['widget']['#cardinality_multiple'])) {
if (!empty($form[$key]['widget']['#title'])) {
$occurrence = 1;
$form[$key]['widget']['#title'] .= _label_help_attach_message($content, '#title');
$use_case = 1;
$form[$key]['widget']['#title'] .= _label_help_attach_message($content, '#title', $use_case);
}
elseif (!empty($form[$key]['widget']['title'])) {
$occurrence = 2;
$use_case = 2;
$form[$key]['widget']['title']['#attributes']['class'][] = 'label';
$form[$key]['widget']['title']['#suffix'] = _label_help_attach_message($content, '#suffix');
$form[$key]['widget']['title']['#suffix'] = _label_help_attach_message($content, '#suffix', $use_case);
}
}
......@@ -122,12 +126,12 @@ function label_help_form_alter(&$form, &$form_state, $form_id) {
) {
// Prepend to existing value when #field_prefix is defined.
if (!empty($item['widget'][0]['#field_prefix'])) {
$occurrence = 3;
$form[$key]['widget'][0]['#field_prefix'] = _label_help_attach_message($content, '#field_prefix') . $item['widget'][0]['#field_prefix'];
$use_case = 3;
$form[$key]['widget'][0]['#field_prefix'] = _label_help_attach_message($content, '#field_prefix', $use_case) . $item['widget'][0]['#field_prefix'];
}
else {
$occurrence = 4;
$form[$key]['widget'][0]['#field_prefix'] = _label_help_attach_message($content, '#field_prefix');
$use_case = 4;
$form[$key]['widget'][0]['#field_prefix'] = _label_help_attach_message($content, '#field_prefix', $use_case);
}
}
......@@ -146,12 +150,12 @@ function label_help_form_alter(&$form, &$form_state, $form_id) {
) {
// Prepend to existing value when #field_prefix is defined.
if (!empty($item['widget']['#field_prefix'])) {
$occurrence = 5;
$form[$key]['widget']['#field_prefix'] = _label_help_attach_message($content, '#field_prefix') . $item['widget']['#field_prefix'];
$use_case = 5;
$form[$key]['widget']['#field_prefix'] = _label_help_attach_message($content, '#field_prefix', $use_case) . $item['widget']['#field_prefix'];
}
else {
$occurrence = 6;
$form[$key]['widget']['#field_prefix'] = _label_help_attach_message($content, '#field_prefix');
$use_case = 6;
$form[$key]['widget']['#field_prefix'] = _label_help_attach_message($content, '#field_prefix', $use_case);
}
}
......@@ -162,12 +166,12 @@ function label_help_form_alter(&$form, &$form_state, $form_id) {
) {
// Prepend to existing value when #field_prefix is defined.
if (!empty($item['widget']['value']['#field_prefix'])) {
$occurrence = 7;
$form[$key]['widget']['value']['#field_prefix'] = _label_help_attach_message($content, '#field_prefix') . $item['widget']['value']['#field_prefix'];
$use_case = 7;
$form[$key]['widget']['value']['#field_prefix'] = _label_help_attach_message($content, '#field_prefix', $use_case) . $item['widget']['value']['#field_prefix'];
}
else {
$occurrence = 8;
$form[$key]['widget']['value']['#field_prefix'] = _label_help_attach_message($content, '#field_prefix');
$use_case = 8;
$form[$key]['widget']['value']['#field_prefix'] = _label_help_attach_message($content, '#field_prefix', $use_case);
}
}
// Special case for containers with a details widget, specified
......@@ -185,12 +189,12 @@ function label_help_form_alter(&$form, &$form_state, $form_id) {
)
) {
if (!empty($item['widget']['#description'])) {
$occurrence = 9;
$form[$key]['widget']['#description'] = _label_help_attach_message($content, '#description') . $item['widget']['#description'];
$use_case = 9;
$form[$key]['widget']['#description'] = _label_help_attach_message($content, '#description', $use_case) . $item['widget']['#description'];
}
else {
$occurrence = 10;
$form[$key]['widget']['#description'] = _label_help_attach_message($content, '#description');
$use_case = 10;
$form[$key]['widget']['#description'] = _label_help_attach_message($content, '#description', $use_case);
}
}
......@@ -209,12 +213,12 @@ function label_help_form_alter(&$form, &$form_state, $form_id) {
)
) {
if (!empty($item['widget'][0]['#description'])) {
$occurrence = 11;
$form[$key]['widget'][0]['#description'] = _label_help_attach_message($content, '#description') . $item['widget'][0]['#description'];
$use_case = 11;
$form[$key]['widget'][0]['#description'] = _label_help_attach_message($content, '#description', $use_case) . $item['widget'][0]['#description'];
}
else {
$occurrence = 12;
$form[$key]['widget'][0]['#description'] = _label_help_attach_message($content, '#label_suffix');
$use_case = 12;
$form[$key]['widget'][0]['#description'] = _label_help_attach_message($content, '#label_suffix', $use_case);
}
}
......@@ -228,14 +232,14 @@ function label_help_form_alter(&$form, &$form_state, $form_id) {
isset($item['widget'][0]['value']) &&
$item['widget'][0]['value']['#type'] == 'datetime'
) {
$occurrence = 13;
$use_case = 13;
$form[$key]['widget'][0]['value']['#title_attributes']['class'][] = 'label';
$form[$key]['widget'][0]['value']['#title'] = $form[$key]['widget'][0]['value']['#title'] . _label_help_attach_message($content, '#title on element');
$form[$key]['widget'][0]['value']['#title'] = $form[$key]['widget'][0]['value']['#title'] . _label_help_attach_message($content, '#title on element', $use_case);
}
elseif (isset($item['widget'][0]['value'])) {
$occurrence = 14;
$form[$key]['widget'][0]['value']['#field_prefix'] = _label_help_attach_message($content, '#field_prefix');
$use_case = 14;
$form[$key]['widget'][0]['value']['#field_prefix'] = _label_help_attach_message($content, '#field_prefix', $use_case);
}
// Special case for link fields when 'Allow link text' option is disabled,
......@@ -243,49 +247,65 @@ function label_help_form_alter(&$form, &$form_state, $form_id) {
// textfield with its field value in the 'uri' key. Therefore, we place
// the message in the #field_prefix.
elseif (isset($item['widget'][0]['uri'])) {
$occurrence = 15;
$form[$key]['widget'][0]['uri']['#field_prefix'] = _label_help_attach_message($content, '#field_prefix');
$use_case = 15;
$form[$key]['widget'][0]['uri']['#field_prefix'] = _label_help_attach_message($content, '#field_prefix', $use_case);
}
// Format used by Client-side Hierarchical Select module.
elseif (isset($item['widget'][0]['target_id']['#title'])) {
$occurrence = 16;
$form[$key]['widget'][0]['target_id']['#label_suffix'] = _label_help_attach_message($content, '#label_suffix');
$use_case = 16;
$form[$key]['widget'][0]['target_id']['#label_suffix'] = _label_help_attach_message($content, '#label_suffix', $use_case);
}
elseif (isset($item['widget'][0]['#title'])) {
$occurrence = 17;
$form[$key]['widget'][0]['#label_suffix'] = _label_help_attach_message($content, '#label_suffix');
$use_case = 17;
$form[$key]['widget'][0]['#label_suffix'] = _label_help_attach_message($content, '#label_suffix', $use_case);
}
// Eg, Select lists.
elseif (isset($item['widget']['#title'])) {
$occurrence = 18;
$form[$key]['widget']['#label_suffix'] = _label_help_attach_message($content, '#label_suffix');
$use_case = 18;
$form[$key]['widget']['#label_suffix'] = _label_help_attach_message($content, '#label_suffix', $use_case);
}
elseif (isset($item['widget']['target_id']['#title'])) {
$occurrence = 19;
$form[$key]['widget']['target_id']['#label_suffix'] = _label_help_attach_message($content, '#label_suffix');
$use_case = 19;
$form[$key]['widget']['target_id']['#label_suffix'] = _label_help_attach_message($content, '#label_suffix', $use_case);
}
else {
$occurrence = 20;
$use_case = 20;
$fallback_use_case = TRUE;
}
}
// Move comments to the top for other field forms that
// are specifically configured.
else {
$occurrence = 21;
$form[$key]['widget']['#label_suffix'] = _label_help_attach_message($content, '#label_suffix');
$use_case = 21;
$fallback_use_case = TRUE;
}
// Add a fallback use case to place message in the element label.
if ($fallback_use_case) {
$form[$key]['widget']['#label_suffix'] = _label_help_attach_message($content, '#label_suffix', $use_case);
if ($debug) {
$form["debug_$key"] = [
'#markup' => "Label Help Debug: Fallback placement: $key (use case $use_case)",
'#weight' => (int) $form[$key]['#weight'] - 1,
];
}
}
$debug = Settings::get('label_help_debug', FALSE);
if ($occurrence && $debug) {
$form["debug_$key"] = [
'#markup' => '<details><summary>' . "($occurrence) $key" . '</summary><pre>' . var_export($form[$key], TRUE) . '</pre></details>',
'#weight' => (int) $form[$key]['#weight'] - 1,
];
if ($use_case && $debug_dump) {
if (empty($one_time_debug_warning)) {
dump("Note: the following variable dump(s) may prevent proper page rendering. This is expected behavior.");
$one_time_debug_warning = TRUE;
}
if ($use_case == 20) {
dump("Label Help Debug: $key (use case $use_case) UNKNOWN FIELD TYPE!");
}
else {
dump("Label Help Debug: $key (use case $use_case)");
}
dump($item);
}
}
}
......@@ -360,16 +380,20 @@ function label_help_theme_suggestions_label_help(array $variables) {
* @param string $placement
* The Form API property name where the label is ultimately rendered. E.g.
* '#title', '#suffix', '#label_suffix', '#field_prefix', '#description'.
* @param int $use_case
* The integer indicating which widget type was identified by the Label
* Help placement logic in label_help_form_alter().
*
* @return string
* The rendered content.
*/
function _label_help_attach_message($content, $placement) {
function _label_help_attach_message($content, $placement, $use_case = 0) {
// Provide themeable markup for the help text.
$renderer = \Drupal::service('renderer');
$debug = Settings::get('label_help_debug', FALSE);
$status = ($use_case !== 20) ? '✅' : '❌';
if ($debug) {
$content = '✅ ' . $content . ' ' . $placement;
$content = "$status $content $placement (use case $use_case)";
}
$element = [
'#theme' => 'label_help',
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment