Skip to content

Helper Functions

Kompass registers a set of global helper functions (in src/Helpers/helpers.php) that are available everywhere — Blade views, controllers, Livewire components, and service classes — without any use import. They are the everyday API you reach for inside block views and frontend templates.

All helpers are guarded with function_exists(), so you can safely override any of them in your own application.


Fields & meta

These read values out of a block's datafield collection and its meta. They are the functions you use most when building a block's Blade view.

get_field()

php
get_field(string $type, $data, ?string $class = null, ?string $size = null, mixed $default = null): mixed

Returns the value of the first datafield matching $type. For image / gallery fields it returns rendered <picture> HTML (using $class and $size); for video / poster it returns the file URL; for everything else, the raw stored value.

blade
@php
    $title = get_field('text', $item->datafield);
    $title = get_field('text', $item->datafield, default: 'Untitled');
    $image = get_field('image', $item->datafield, 'w-full rounded', 'thumbnail');
@endphp

get_fields()

php
get_fields(string $type, $data): array

Like get_field(), but returns all matching field values as an array — use it for repeatable fields (e.g. a list of links).

php
$links = get_fields('link', $item->datafield);

get_field_as()

php
get_field_as(string $type, $data, string $cast = 'string', mixed $default = null): mixed

get_field() with a type cast. $cast is one of int, bool, array, object, or string.

php
$limit    = get_field_as('limit', $item->datafield, 'int', 10);
$isActive = get_field_as('active', $item->datafield, 'bool', false);

get_meta()

php
get_meta(object $item, string $key, mixed $default = null): mixed

Reads a per-block setting from the block's meta, with a fallback. Equivalent to $item->getMeta($key) but null-safe and with a default.

blade
<div class="card {{ get_meta($item, 'css-classname', 'bg-white') }}">

get_thumbnails()

php
get_thumbnails($id_media, $class = null, $size = null): string

Renders a media file (by ID) as responsive <picture> HTML, or an empty string if the file is missing.


Blocks & editor

Utilities for working with block structure and the Kompass Editor content format.

block_grid_classes()

php
block_grid_classes(mixed $item): array

Returns ['gridCols' => 'md:grid-cols-N', 'colSpan' => 'md:col-span-N'] derived from the block's layoutgrid — used by Layout Blocks to build their responsive grid.

wysiwyg_blocks()

php
wysiwyg_blocks(mixed $item = null, $field = null): array

Normalises stored WYSIWYG content into the canonical render array. Accepts a block item, a raw field, an array, or a string, and transparently upgrades legacy Editor.js data. Use it before rendering rich text.

php
$blocks = wysiwyg_blocks($item);

block_registry() / field_registry()

php
block_registry(): BlockTypeRegistry
field_registry(): FieldTypeRegistry

Return the single-source-of-truth registries for the configured block types and field types.

to_compiled_array() / to_compiled_object()

php
to_compiled_array(mixed $input): array
to_compiled_object(mixed $input): object

Normalise blocks data of any shape into a flat array, or a wrapped object, respectively.


Relationship queries

These power the Relationship Block — resolving registered sources and running their queries. See that page for the full data model and item-view contract.

query_models()

php
query_models(): array

Returns the registered queryable sources, keyed by source key. Merges the config-defined kompass.query_models entries with the admin-managed query_sources table (config wins on a key collision). Database sources are filtered against the kompass.query_source_models allow-list.

kompass_query()

php
kompass_query($block): Collection

Runs a Relationship block's configured query and returns the matched records. Auto mode applies status/scope, ordering, direction, and limit; manual mode returns the curated selection in saved order. Returns an empty collection when nothing is configured.

kompass_query_candidates()

php
kompass_query_candidates(string $modelKey, int $limit = 50, ?string $search = null): Collection

Selectable records for the manual-selection picker. An optional $search term filters server-side across the source's display_fields.

kompass_query_url()

php
kompass_query_url(string $modelKey, $record): ?string

Builds a record's frontend URL from the source's url_pattern ({slug} is replaced with the record's slug). Returns null when the source has no pattern.

kompass_query_label()

php
kompass_query_label(string $modelKey, $record): string

A human label for a record — the source's display_fields joined with ·, falling back to #id.

kompass_apply_scope()

php
kompass_apply_scope($query, string $modelClass, ?string $scope): void

Applies a named Eloquent local scope to a query, but only when the model actually defines it — a guard against calling arbitrary methods from admin-supplied config.


Settings & SEO

setting()

php
setting($key = null, $default = null)

Reads a global setting by its dot-notated group.key. Called with no argument it returns the whole settings collection.

blade
{{ setting('global.webtitle', 'Kompass') }}

See Global Settings for the available groups.

seo()

php
seo(): SeoService

Returns the fluent SeoService for building <title>, <meta>, and Open Graph / Twitter tags. See SEO for the full method reference and how the default page.blade.php uses it.

php
seo()->title($page->title)->description($page->meta_description);

Assets & images

kompass_asset()

php
kompass_asset($path, $secure = null): string

Builds a URL to a published Kompass asset under vendor/kompass/assets/. See Publishing Assets.

getImageID() / getImageUrl()

php
getImageID($id, $sizeKey = null)
getImageUrl($url, $sizeKey = null)

Resolve a processed image (responsive size variant) by media ID or by source URL, respectively. See Media Library for image sizes and conversions.


Overriding a helper

Because every helper is wrapped in if (! function_exists(...)), you can define your own version earlier in the boot process (e.g. in an autoloaded file) to replace Kompass's implementation.

Released under the MIT License.