Automatically alter "Content type" filter based on "Is Micro-content" filter

Created on 6 September 2024, 6 months ago
Updated 8 September 2024, 6 months ago

Problem/Motivation

If I add the "Is Micro-content" filter to the content admin view, the micro-content types are still available from the "Content type" dropdown list.

Steps to reproduce

Install umami
Make Recipe a micronode type and re-save article and page as non micro-content.
Add "Is Micro-content (=No)) as a filter to the content admin view
Go to the content admin page
The view will never show recipes, but recipe is still an option in the Content type dropdown

Proposed resolution

Obviously I could reconfigure the "Content type" filter to exclude Recipe or to include only Article and Page. That would have to be updated every time a new content type is added though.

Wouldn't it be great if the Content type filter were altered automagically?

Remaining tasks

User interface changes

Fewer useless filter options

API changes

nope

Data model changes

nope

✨ Feature request
Status

Fixed

Version

1.1

Component

Code

Created by

πŸ‡ΊπŸ‡ΈUnited States danflanagan8 St. Louis, US

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

Merge Requests

Comments & Activities

  • Issue created by @danflanagan8
  • πŸ‡ΊπŸ‡ΈUnited States danflanagan8 St. Louis, US

    I'm going to put in a real MR with some test coverage, but the hook is going to look something like this:

    /**
     * Remove types from exposed filters when 'Is Micro-content' filter is in use.
     *
     * Implements hook_form_views_exposed_form_alter().
     */
    function micronode_form_views_exposed_form_alter(&$form, FormStateInterface $form_state, $form_id) {
      if ($view = $form_state->getStorage()['view']) {
        $filters = $view->getHandlers('filter');
        if (isset($filters['micronode_is_microcontent'])) {
          $return_microcontent = $view->display_handler->getHandler('filter', 'micronode_is_microcontent')->value;
          $allowed_bundles = array_keys(micronode_get_node_types($return_microcontent));
          foreach ($filters as $id => $definition) {
            if (isset($definition['exposed']) && $definition['exposed'] && $definition['plugin_id'] == 'bundle') {
              foreach ($form[$id]['#options'] as $key => $value) {
                if (is_string($value) && !in_array($key, $allowed_bundles, TRUE)) {
                  unset($form[$id]['#options'][$key]);
                }
              }
            }
          }
        }
      }
    }

    This is a refactored version of something originally conceived of and written by the @allie micka. We need to make sure to give credit if and when it comes to that! I love this feature, though, and hopefully it's not too hard to sell. :)

  • Merge request !8Views filter alter β†’ (Merged) created by danflanagan8
  • Status changed to Needs review 6 months ago
  • πŸ‡ΊπŸ‡ΈUnited States danflanagan8 St. Louis, US

    I put in an MR with the hook and some test coverage. It looks like tests aren't set up to run on Gitlab for this project, but the MicronodeViewsTest passes locally for me.

  • First commit to issue fork.
  • πŸ‡ͺπŸ‡ΈSpain marcoscano Barcelona, Spain
  • Pipeline finished with Skipped
    6 months ago
    #277316
  • Status changed to Fixed 6 months ago
  • πŸ‡ͺπŸ‡ΈSpain marcoscano Barcelona, Spain

    Looks good to me, thanks for contributing! πŸ‘

  • Automatically closed - issue fixed for 2 weeks with no activity.

Production build 0.71.5 2024