daun/statamic-utils

A collection of utilities for use in Statamic projects

1.5.0 2025-03-14 11:16 UTC

This package is auto-updated.

Last update: 2025-03-14 11:16:25 UTC


README

A collection of utilities I use in Statamic projects.

Installation

Install the package via composer:

composer require daun/statamic-utils

Registration

Modifiers, Tags, Scopes, etc. need to be registered in your app's service provider.

class AppServiceProvider extends ServiceProvider
{
    public function boot(): void
    {
        \Daun\StatamicUtils\Modifiers\ToIterable::register();
        \Daun\StatamicUtils\Scopes\Image::register();
    }
}

Modifiers

Is String

Check if a value is a string.

{{ if some_var | is_string }}

Asset

Return or find an asset by id or url.

{{# Fetch asset if url was passed #}}
{{ image = image | asset }}

Count Safe

Count the number of items in an array or iterable. Returns 0 for null values and 1 for non-iterable values.

{{ if locations | count_safe }} ... {{ /if }}

Max

Return the highest value in an array or collection.

{{ large = sizes | max }}

Min

Return the lowest value in an array or collection.

{{ small = sizes | min }}

P 2 Br

Convert paragraph tags to line breaks.

<p>{{ rich_text | p2br }}</p>

Push

Push an item onto an array or collection.

{{ items = (items | push:{newitem}) }}

To Int

Convert a value to an integer. Special case: converts a mixed array to an array of integers.

{{ number_array = mixed_array | to_int }}

To Float

Convert a value to a float. Special case: converts a mixed array to an array of floats.

{{ number_array = mixed_array | to_float }}

To Iterable

Wrap a value in an array if it is not already iterable.

Locations: {{ (locations ?? location) | to_iterable | pluck('title') | list }}

Query Scopes

Apply query scopes to narrow down query results.

  • Published: Filter out unpublished entries
  • Image: Filter assets that are images (pixel + vector)
  • ImagePixel: Filter assets that are pixel images (jpeg, png, gif, etc)
  • ImageVector: Filter assets that are vector images (svg)
  • ImageOrVideo: Filter assets that are images or videos
  • Video: Filter assets that are video files
  • Audio: Filter assets that are audio files

Search Filters

Classes for filtering entries for search indexing.

  • Published: Filter out unpublished entries.
  • All: Include all entries, regardless of their published status.
return [
    'indexes' => [
        'articles' => [
            'searchables' => 'collection:articles',
            'filter' => \Daun\StatamicUtils\Search\Filters\Published::class,
        ]
    ]
];

Search Transformers

Classes for transforming fields for search indexing.

  • BardText: Extract plain text from a Bard field.
  • RelationshipTitle: Map relationship fields to an array of titles.
return [
    'indexes' => [
        'articles' => [
            'searchables' => 'collection:articles',
            'transformers' => [
                'content' => \Daun\StatamicUtils\Search\Transformers\BardText::class,
                'categories' => \Daun\StatamicUtils\Search\Transformers\RelationshipTitle::class,
            ]
        ]
    ]
];

Cache

Query Params

Get an up-to-date list of marketing query params to ignore when caching a page.

// config/statamic/static_caching.php

return [
    'disallowed_query_strings' => Daun\StatamicUtils\Cache\QueryParams::toIgnore()
];

Utilities

Control Panel

Translations

Ensure the existence of customized Create Entry buttons for all data types. Trows an exception if a collection or taxonomy is missing the required translation key.

\Daun\StatamicUtils\ControlPanel\Translations::ensureCreateButtonLabels();

License

MIT