ElementsCMSItem.php
Represents a single CMS item loaded from a Markdown file with front matter. Provides structured access to metadata, raw and rendered content, file paths, and relationships to other items.
Constructor
__construct(ElementsCMS $cms, array $data = [])
__construct(ElementsCMS $cms, array $data = [])Creates a new CMS item.
$cms (
ElementsCMS): The CMS instance.$data (
array): The data for the CMS item.
Static Methods
static load(ElementsCMS $cms, string $filepath): ?self
static load(ElementsCMS $cms, string $filepath): ?selfLoad a CMS item from a file.
$cms (
ElementsCMS): The CMS instance.$filepath (
string): Path to the file.Returns:
ElementsCMSItem|null– Loaded item or null if not found.
Instance Methods
setOptions(array $options): self
setOptions(array $options): selfSets runtime options for the item.
$options (
array): Option key/value pairs.Returns:
$thisfor method chaining.
getOption(string $key, $default = null)
getOption(string $key, $default = null)Gets an option value by key.
$key (
string): Option name.$default (
mixed): Default value if not found.Returns: Mixed option value.
slug(): string
slug(): stringReturns the item’s slug (URL-friendly identifier).
title(): string
title(): stringReturns the item’s title.
date(string $format = 'F j, Y'): string
date(string $format = 'F j, Y'): stringReturns the item's date in the given format.
$format (
string): Date format (default'F j, Y').
datePublished(string $format = 'F j, Y'): string
datePublished(string $format = 'F j, Y'): stringReturns the item's published date.
dateModified(string $format = 'F j, Y'): string
dateModified(string $format = 'F j, Y'): stringReturns the item's last modified date.
featured(): bool
featured(): boolChecks if the item is marked as featured.
status(): string
status(): stringReturns the item's status (e.g., published/draft).
body(): string
body(): stringReturns the rendered HTML body content.
rawBody(): string
rawBody(): stringReturns the raw markdown body content.
image(): ?string
image(): ?stringReturns the item's image URL, if available.
excerpt(int $words = 30): string
excerpt(int $words = 30): stringReturns an excerpt of the body, limited to the specified word count.
meta(string $key, $default = null): mixed
meta(string $key, $default = null): mixedFetches a value from the item's metadata/front-matter.
$key (
string): Meta key.$default (
mixed): Default if key is missing.
attach(string $key, $value): self
attach(string $key, $value): selfAttach related data to the item (e.g., relations, computed properties).
get(string $key, $default = null): mixed
get(string $key, $default = null): mixedReturns any value from the item data or metadata.
__call($method, $arguments)
__call($method, $arguments)Allows dynamic method access to metadata fields.
file(): string
file(): stringReturns the full path to the file.
fileName(): string
fileName(): stringReturns the base filename (without extension).
url(): string
url(): stringReturns the item’s URL.
lastModified(string $format = 'F j, Y'): string
lastModified(string $format = 'F j, Y'): stringReturns the file's last modified date.
toArray(): array
toArray(): arrayReturns all item data as an associative array.
with(string ...$relations): self
with(string ...$relations): selfDeclare relations (one-to-one or one-to-many) to eager-load.
withOne(string ...$relations): self
withOne(string ...$relations): selfDeclare one-to-one relations to eager-load.
withMany(string ...$relations): self
withMany(string ...$relations): selfDeclare one-to-many relations to eager-load.
loadRelations(): self
loadRelations(): selfLoads all defined relations for the item.
Usage Example
Last updated
Was this helpful?

