Allow to enable description toggle in custom forms

Created on 15 September 2023, over 1 year ago
Updated 14 August 2024, 4 months ago

Problem/Motivation

Currently description toggle is only for content forms and forms where routes had been declared with hook_gin_content_form_routes().

As currently, to my knowledge, there is no standard element/API in core to provide tooltips, I would like to use Gin's description toggle on other forms.

Proposed resolution

Enable description toggle based on key in the form element.

Feature request
Status

RTBC

Version

3.0

Component

Code

Created by

🇫🇷France Grimreaper France 🇫🇷

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

Comments & Activities

  • Issue created by @Grimreaper
  • @grimreaper opened merge request.
  • Issue was unassigned.
  • Status changed to Needs review over 1 year ago
  • 🇫🇷France Grimreaper France 🇫🇷

    With the MR, if in an element there is:

    [
              '#title' => 'my title',
              '#description' => 'my description',
              '#description_toggle' => TRUE,
            ];
    

    The element will have the toggle.

    I am currently poking something in conjunction with Gin Layout Builder for select list. Maybe other form elements will require update.

  • 🇫🇷France Grimreaper France 🇫🇷

    Attaching patch for Composer usage.

  • 🇮🇳India shubham_jain

    Hi @Grimreaper,
    && $variables['element']['#description_toggle']
    can you explain why you have added this because isset is already checking that the value is set or not. So, I don't think we need this code.

  • 🇫🇷France Grimreaper France 🇫🇷

    Hi @shubham_jain,

    The isset tests that the key exists, it does not check if it is TRUE or FALSE.

  • Status changed to RTBC 4 months ago
  • 🇺🇸United States jrockowitz Brooklyn, NY

    I think this is a great idea.

    The code is correct in using...

    (isset($variables['element']['#description_toggle']) && $variables['element']['#description_toggle'])

    ...because the $variables['element']['#description_toggle'] can be defined but set to FALSE.

    I will mark this RTBC unless the maintainer needs test coverage or wants someone to take a different approach.

    BTW, if someone wants to enable the description toggle for all admin forms via the Gin admin, they would need to use the patch and the custom code below.

    /**
     * Implements template_preprocess_HOOK() for form_element.
     */
    function MY_MODULE_preprocess_form_element(&$variables) {
      _MY_MODULE_preprocess_description_toggle($variables);
    }
    
    /**
     * Implements template_preprocess_HOOK() for datetime_wrapper.
     */
    function MY_MODULE_preprocess_datetime_wrapper(&$variables) {
      _MY_MODULE_preprocess_description_toggle($variables);
    }
    
    /**
     * Implements hook_preprocess_details().
     */
    function MY_MODULE_preprocess_details(array &$variables): void {
      _MY_MODULE_preprocess_description_toggle($variables);
    }
    
    /**
     * Implements template_preprocess_HOOK() for fieldset.
     *
     * @see MY_MODULE_preprocess_fieldset
     */
    function MY_MODULE_preprocess_fieldset(&$variables) {
      _MY_MODULE_preprocess_description_toggle($variables);
    }
    
    /**
     * Implements template_preprocess_HOOK() for form_element.
     *
     * @see \Drupal\gin\GinDescriptionToggle
     * @see gin_preprocess_form_element()
     * @see gin_preprocess_datetime_wrapper()
     * @see gin_preprocess_details()
     * @see gin_preprocess_fieldset()
     */
    function _MY_MODULE_preprocess_description_toggle(&$variables) {
      /** @var \Drupal\Core\Theme\ThemeManagerInterface $theme_manager */
      $theme_manager = \Drupal::service('theme.manager');
      if ($theme_manager->getActiveTheme()->getName() === 'gin') {
        $variables['element']['#description_toggle'] = TRUE;
      }
    }
    
Production build 0.71.5 2024