Skip to content Docs index for AI agents (llms.txt)
Laravel
2.0

Versions

Advanced UsageTraits

Traits

Extending Block and Page functionality with reusable traits in Laravel Storyblok.

Updated by Ric Le Poidevinf2f03b71 min read


You can extend the functionality of your Blocks by adding methods to individual ones or your DefaultBlock but this limits their reuse between projects.

Another option is to use Traits and we have added some helpful features to get the most out of them. This is how we implement features like converting Markdown.

Running methods when initialised

Maybe you need a method that truncates selected strings when a Block is created. You add this to a trait and want the method to be run automatically. This can be done by adding a method called initTraitName to your trait as follows.

php
namespace App\Storyblok\Traits;

use Illuminate\Support\Str;

trait TruncateStrings
{
	protected array $toTruncate = [];

	/**
     * A fairly pointless method to truncate selected content to 10 words.
     * It is run when automatically when this Trait is used on a class.
     */
    public function initTruncateStrings() {
		if (!empty($this->toTruncate)) {
			foreach ($this->toTruncate as $field) {
					$this->content[$field] = Str::words($this->content[$field], 10);
			}
		}
	}
}
php
<?php

namespace App\Storyblok;

use App\Storyblok\Traits\TruncateStrings;

class SomeComponent extends \App\Storyblok\DefaultBlock
{
    use TruncateStrings;

    protected $toTruncate = ['introduction'];
}