Improve Webform categorization to support assigning multiple categories and default categories.

Created on 7 February 2023, over 1 year ago
Updated 16 February 2024, 4 months ago

Problem/Motivation

As a form builder, I would like to apply multiple categories to webforms so that I can filter by a webform based of different categories. For example, a webform could be used for event registration with a payment. Currently, the webform can only be categorized as 'Event' or 'Payment'

It is also important to note that a webform's category is stored as a simple string, making it difficult to translate. No one has complained about this limitation because I think most websites manage webforms using the site's default language.

Steps to reproduce

Assign a webform to a category.

Notes

  • Webform categories are stored as strings.
  • Webform category must be assigned to webform for it to appear in the webform category dropdown.
  • Webform categories are pulled from all webforms. This creates a performance issue that requires all webforms to be loaded and cached.. @see #3239783: Cache webform categories β†’

Proposed resolution

Support multiple categories.

Below are three potential approaches.

APPROACH 1:

Update webform category to support multiple values.

Pro: Simplest solution

Cons: Makes it difficult to create multiple custom categories because the select other element only supports one category

APPROACH 2:

Improve the overall management of webform categories and add default categories to webform.settings.yml

Pro: Allows only webform administrators to create default webform categories while allowing form builders to create custom categories.

Cons: Performance issue potential still exist. Translations may still be an issue.

APPROACH 3:

Move webform categorization to use a dedicated 'webform_categories' taxonomy.

Pro: Taxonomies are intended for categorization and are easier to translate.

Cons: Moving simple configuration settings to a taxonomy (aka content entity) will make it difficult to move webform categories across multiple webform sites. Add additional complexity,

Remaining tasks

  • Determine approach
  • Build approach
  • Write tests
  • Commit code

User interface changes

Selecting a webform category will support multiple values.

API changes

Webform category setting will support multiple categories.

Data model changes

Webform category supports multiple categories

✨ Feature request
Status

Fixed

Version

6.2

Component

Code

Created by

πŸ‡ΊπŸ‡ΈUnited States jrockowitz Brooklyn, NY

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

Comments & Activities

  • Issue created by @jrockowitz
  • πŸ‡ΊπŸ‡ΈUnited States jrockowitz Brooklyn, NY

    Approach 2: Solves the immediate requirement of supporting multiple categories while leaving the possibility for future improvements open.

  • πŸ‡ΊπŸ‡ΈUnited States FatherShawn New York

    This looks like the best solution because it is portable within webforms

  • πŸ‡ΊπŸ‡ΈUnited States devanbicher

    I agree with approach 2. Portability across webforms seems like a nice advantage. Potential translation issues don't seem as important if that issue hasn't been raised before, but hopefully that is fixable should that situation arise.

  • Status changed to Needs review over 1 year ago
  • πŸ‡ΊπŸ‡ΈUnited States jrockowitz Brooklyn, NY
  • @jrockowitz opened merge request.
  • πŸ‡ΊπŸ‡ΈUnited States jrockowitz Brooklyn, NY
  • The last submitted patch, 7: 3339769-7.patch, failed testing. View results β†’
    - codesniffer_fixes.patch Interdiff of automated coding standards fixes only.

  • πŸ‡ΊπŸ‡ΈUnited States jrockowitz Brooklyn, NY
  • The last submitted patch, 9: 3339769-9.patch, failed testing. View results β†’
    - codesniffer_fixes.patch Interdiff of automated coding standards fixes only.

  • πŸ‡ΊπŸ‡ΈUnited States jrockowitz Brooklyn, NY
  • πŸ‡ΊπŸ‡ΈUnited States jrockowitz Brooklyn, NY
  • Status changed to Needs work over 1 year ago
  • πŸ‡§πŸ‡·Brazil RenatoG Campinas

    Seems good. My suggestion if on _webform_update_webform_setting_propertiesat includes/webform.install.inc we we're using two ifs on this piece:

    if (isset($properties['category'])) {
      $properties['categories'] = [];
      if (!empty($properties['category'])) {
    

    I think we can use the opposite, instead of verifying if (isset($properties['category'])) with the code there, we can verify the opposite if (!isset($properties['category'])) and with early return there, like this:

    if (!isset($properties['category'])) {
      return $properties;
    }
    

    With that we can reduce the code indentation you know? For example:

    From (before)

    function _webform_update_webform_setting_properties(array $properties) {
      // Issue #3339769: Improve Webform categorization to support assigning multiple categories and default categories
      if (isset($properties['category'])) {
        $properties['categories'] = [];
        if (!empty($properties['category'])) {
          $properties['categories'] = array_values(array_filter(array_merge(
            $properties['categories'],
            (array) $properties['category']
          )));
        }
        unset($properties['category']);
      }
    
      return $properties;
    }
    

    To: (new)

    function _webform_update_webform_setting_properties(array $properties) {
      // Issue #3339769: Improve Webform categorization to support assigning multiple categories and default categories
      if (!isset($properties['category'])) {
        return $properties;
      }
    
      $properties['categories'] = [];
      if (!empty($properties['category'])) {
        $properties['categories'] = array_values(array_filter(array_merge(
          $properties['categories'],
          (array) $properties['category']
        )));
      }
      unset($properties['category']);
    
    
      return $properties;
    }
    
  • Status changed to Needs review over 1 year ago
  • πŸ‡§πŸ‡·Brazil RenatoG Campinas

    Patch with the example of suggestion

  • πŸ‡ΊπŸ‡ΈUnited States jrockowitz Brooklyn, NY

    I don't want to do an early return via _webform_update_webform_setting_properties to allow other tickets to alter a webform's properties in the future.

    @see _webform_update_webform_setting_settings()

  • πŸ‡§πŸ‡·Brazil RenatoG Campinas

    Got it, really Makes sense. Good catch

    I just hid #14 in favor of #11

  • πŸ‡ΊπŸ‡ΈUnited States jrockowitz Brooklyn, NY
  • πŸ‡ΊπŸ‡ΈUnited States jrockowitz Brooklyn, NY
  • Status changed to Fixed about 1 year ago
  • Automatically closed - issue fixed for 2 weeks with no activity.

  • πŸ‡¨πŸ‡¦Canada endless_wander

    In case someone finds this thread with same issue as me, the change to 'categories' instead of 'category' broke some entityQuery instances. So must change any conditions for categories from:

    ->condition('category', 'Category Name')

    to:

    ->condition('categories.*', 'Category Name')

    As a side note, it may be nice to add a `hasCategory()` method to the Webform class as well

  • πŸ‡©πŸ‡ͺGermany kle

    Yes endless_wander, this would be nice.. ->hasCategory()

  • πŸ‡§πŸ‡ͺBelgium arno_vgh

    Hi,

    Although I appreciate this adjustment immensely, I would have preferred to see it documented in the change records as it could potentially break existing code and therefore likely requires changes.

    In the past, using $category = $webform->get('category') (assuming a webform could only have one category) will now return NULL. Updating the code to $webform->get('categories') will now return an array of categories.

    (My apologies if this information is already documented somewhere and I overlooked it).

Production build 0.69.0 2024