HELPeR

Helper fixes small annoyances in Drupal core and popular contrib modules, and gives developers a set of services and utilities for everyday module and site building work.

  • Read the full documentation for every helper and the API reference.
  • Submit bug reports and feature suggestions, or track changes, in the issue queue.

Requirements

This module requires Drupal 11.3 or later, or Drupal 12, and these libraries, which Composer installs with it:

The Drush commands need Drush 13 or later. Some helpers only apply when the module they work with is installed, such as Layout Builder, Views, Media or Translation Management Tool, but Helper doesn't require any of them.

Installation

Install as you would normally install a contributed Drupal module. For further information, see Installing Drupal Modules.

Configuration

Helper has no settings page. Each helper is turned on or off in the enabled mapping of the helper.settings configuration, for example with Drush:

drush config:set --input-format=yaml helper.settings enabled.core_form_novalidate true

Some helpers are on when Helper is installed, but stay off on sites that installed it before the helper was added, until they're turned on. See the documentation for each helper's default.

Highlights

  • Better #states in forms. Conditions on multiple selects work, plus regular expression, exclusive or and textarea line conditions.
  • Smarter Views exposed filters. Show field filters only for the content types that have the field, clear filters when they're hidden, and hide selects with nothing to choose.
  • Better Views cache hit rates. Views filtered by bundle are tagged with bundle list cache tags, such as node_list:article, so saving a page no longer clears every view of articles.
  • Bulk forms that only offer what you can do. Actions the user can't perform are removed from Views bulk forms.
  • Layout Builder conveniences. A Layout operation link that becomes the default operation in listings, and the featured action in the Navigation top bar, when an entity's layout is overridden or its default layout is empty. Inline blocks are kept out of default layouts.
  • Developer APIs, such as ContentEntityUpdater for batch updates of content in post-update and deploy hooks, and helpers for arrays, render elements, entities, views, menus, config and files.

Configurable helpers

These can each be turned on or off, as described in Configuration.

Forms and fields

  • core_states_conditions_fix: fixes and extends #states conditions for multiple-select fields, and adds regular expression, exclusive or and textarea line conditions.
  • core_form_novalidate: turns off HTML5 form validation.
  • core_entity_reference_hide_empty: hides entity reference fields with nothing to select.
  • core_text_long_textfield_widgets: single-line widget for formatted long text fields.
  • core_text_textarea_widgets: textarea widgets for plain text fields.
  • core_hide_text_format_help_single_format: hides text format help when only one format is allowed (Drupal before 11.4).
  • core_hide_layout_providers: hides layouts from the listed modules.
  • cacheable_binary_file_response: lets reverse proxies and CDNs cache public files that Drupal sends itself.
  • redirect_entity_4xx_to_edit: sends editors to the edit form when viewing an entity gives a 403 or 404.

Views

  • views_exposed_states_bundle_filter_fields: shows field filters only for bundles that have the field.
  • views_exposed_states_reset: clears exposed filters when #states hides them.
  • views_exposed_select_hide_empty: hides exposed selects with nothing to choose.
  • views_cache_tags_bundles: bundle list cache tags for views filtered by bundle.
  • views_filter_bulk_action_options: removes bulk actions the user can't perform.
  • views_fix_sort_indicator_direction: shows the correct direction sort arrow on views tables in the listed themes and their sub-themes, by default Claro and Default Admin.

Layout Builder, media, menus and translation

  • layout_builder_entity_operation: adds a Layout operation link to entities that can override their layout.
  • layout_builder_entity_operation_default_overridden: makes Layout the default operation in listings, and the featured action in the Navigation top bar, when the layout is overridden or the default layout is empty.
  • layout_builder_inline_block_validation: prevents inline blocks in default layouts.
  • media_oembed_iframe_title: adds a title to oEmbed iframes for accessibility.
  • menu_links_prefer_deepest: prefers the deepest menu link for an entity.
  • navigation_create_links: adds media types and other entity types with an add form to the Navigation module's Create menu, all bundles in a drop-down or chosen ones as links.
  • tmgmt_hide_if_not_multilingual: hides Translation Management until the site has a second language.

For developers

Much of Helper is a toolkit for writing modules, update hooks and site code.

Services

  • ContentEntityUpdater: updates content entities in batches from a hook_post_update_NAME() or Drush's hook_deploy_NAME(), with update() and updateFieldPropertyValue(). Includes entities in the trash.
    \Drupal::classResolver(ContentEntityUpdater::class)
      ->update($sandbox, 'taxonomy_term', function (TermInterface $term) {
        $term->setName(mb_strtolower($term->getName()));
        return TRUE;
      });
    
  • Config: imports and re-exports config files, directories and modules, with importModule(), importFile() and reExportModule().
  • CurrentEntity: gets the entity of the current page with fromRouteMatch(), on any of its routes: canonical, edit, revisions, preview, Layout Builder and REST, falling back to Metatag's hook_metatag_route_entity(). Optionally limited to route names matching a pattern, and cached for the request.
  • EntityHelper: checks entities and gets their state, with matchesPath(), hasValue() and getState().
  • EntityType: works with bundles and entity options, with getBundleOptions(), getEntityOptions(), accessBundles() and getBundlesReferencingBundle().
  • File: creates or reuses file entities, builds data URIs and checks whether a URI is public, with createOrReuseFromUri(), getDataUri() and isPublicUri().
  • InstallProfile: switches the site's install profile, with switch().
  • LayoutBuilder: checks and sets entity layouts, with isEntityLayoutOverridden(), isLayoutEmpty() and setEntityLayout().
  • MediaHelper: gets a media item's file and allowed file extensions, with getFile() and getFileExtensions().
  • Menu: builds menus from menu names, links and link fields, with buildMenu(), buildLinkFieldAsMenu() and getMenuLinkForEntity().
  • PathautoHelper: checks whether an entity has an automatic alias, with hasAutomaticAlias().
  • StringField: changes string and text fields that already have data, keeping it: their length with changeLength(), to or from formatted text with changeToText() and changeFromText(), and to their long (unlimited length) types with changeToLong().
  • CustomField: changes Custom Field string subfields that already have data, keeping it: their length with changeLength(), and to string_long with changeStringToLong().
  • TextFormat: changes the HTML tags and attributes a text format allows, with addTag(), removeTag() and isTagAllowed().
  • Theme: checks, sets and temporarily switches themes, with isActive(), switch() and switchBack().
  • ViewHelper: reads a view's bundle filters, with getBundleFilters() and getEntityBundlesFromFilter().

Static utilities

  • ArrayHelper: changes arrays while keeping their keys, with insertAfterKey(), replaceKey(), addUniqueValue(), removeValue() and chunkEvenly().
  • Field: finds duplicate values in a field, with getDuplicateValues().
  • Html: removes HTML comments, with removeComments().
  • Plugin: gets plugin managers and plugin options for forms, with getManager() and getOptions().
  • RenderElement: changes render arrays, with attachLibrary(), attachHeader(), attachStyle(), mergeAccess(), wrap() and applyToChildren().
  • Utility: runs batches and other small tasks, with runBatch(), runSandboxBatch(), registerUniqueShutdownFunction() and matchString().

Plugins

  • Date only field widget (helper_timestamp_date): a date picker without a time for timestamp and created fields.
  • Entity select form element (helper_entity_select): a select list of entities.
  • Node field block (helper_node_field): shows a field of the current node in any block region.
  • Context rendered entity block (helper_context_entity): shows the current entity in a chosen view mode.
  • Unique field values constraints (UniqueFieldValues and HelperEntityFieldUniqueValues): reject duplicate values in a field.
  • Helper Logging Mailer (helper_logger): logs emails instead of sending them.
  • Tag based Views cache with bundle cache tags: used by the views_cache_tags_bundles helper.

Drush commands

  • entity:count: counts entities by bundle.
  • entity:field:value, entity:field-type:value, entity:field:empty and entity:field:minimum-values: find entities by their field values.
  • entity:field:taxonomy: summarizes taxonomy term usage.
  • redirects: summarizes redirects.
  • install-profile:switch: switches the install profile.
  • module:schema-version:get, set, delete and cleanup: manage module schema versions, including leftovers from deleted modules.
  • module:post-update:reset: lets a post-update hook run again.
  • cache:clear libraries and cache:clear bootstrap: clear the library definitions and CSS/JS, or the bootstrap cache.

Data files

JsonFile, YamlFile and ComposerFile read and write JSON and YAML files, and the site's composer.json, with readData() and writeData(). They're meant for important files that are read often, even on every request:

  • They can be used early, even in settings.php before Drupal has booted.
  • They cache the decoded contents until the file changes: in APCu through core's file cache before Drupal boots, and in a chained fast (APCu) cache bin after.

For example, to read a JSON file in settings.php:

$domains = (new \Drupal\helper\File\JsonFile($app_root . '/../domains.json'))->readData();

Twig

  • format_bytes filter: formats a number of bytes as a readable size.
  • file_data_uri() function: embeds a file as a data URI.

Other

  • is_multilingual route requirement: limits a route to sites with more than one language.
  • module_dependency service tag: removes a service when a module it needs isn't installed.
  • Sub-theme region inheritance from the base theme.
  • Test traits with assertions for render elements and their cache metadata, file usage, and queues.
Supporting organizations: 
Development and maintenance
Feature development

Project information

Releases