TasticFieldHandler

The standard data sources in Frontastic are contained in what we call Streams. For example, product-list and content are both Stream types. If a Tastic is defined to retrieve a Stream of a certain type, it'll be provided with the corresponding data automatically at the time of rendering. You can extend this mechanism with your own Stream types to provide data to Tastics in a similar way.

You'll be using what's called the Tastic Field Handler. It's been known as Custom Field Handler but the Class is TasticFieldHandler.

To provide a Custom Stream type, you'll need to follow the steps below to register the Handler in the Frontastic PHP stack:

1
Provide an Implementation
In order to handle a Custom Stream you'll need to provide an implementation of Frontastic\Catwalk\FrontendBundle\Domain\TasticFieldHandlerV2 which is the generalized interface for handling all field types and contains two methods:
namespace Frontastic\Catwalk\FrontendBundle\Domain;

use Frontastic\Catwalk\ApiCoreBundle\Domain\Context;

abstract class TasticFieldHandlerV2
{
    /**
     * @return string
     */
    abstract public function getType(): string;

    /**
     * @param mixed $fieldValue
     * @return mixed Handled value
     */
    abstract public function handle(Context $context, Node $node, Page $page, $fieldValue);
}

The getType() method should return the type identifier for your Stream type.

You must prefix Custom Stream types with your project ID in the form<project-id>-<stream-type>, for example, awesomeshop-stores if your project is awesomeshop and your StreamType will provide information about local stores.

The second method handle() performs the actual work of handling a Stream of your type. It's called by Frontastic as soon as a Tastic requires a Stream of your type.

As parameters, it receives the current application Context, Node, Page and the $fieldValue which was assigned by the configuring user in Backstage.

PLEASE NOTE: We're still working on this feature. It's not yet possible for users in Backstage to assign values to Custom Stream fields. But even without this possibility Custom Streams can be useful to provide data to a Tastic.

The handle() method is supposed to return the value of the Stream that will then be given to the Tastic that required it. It can be an arbitrary value type except for one constraint: It must be serializable to JSON (that excludes, for example, resource types). Ideally you will return either

  • a simple data object
  • an array of scalar values of simple array objects
  • null

or a combination of these.

You may notice that this is also called TasticFieldHandlerV2. If you previously implemented the TasticFieldHandler and it's not using V2, you can use the TasticFieldHandlerAdapter if you don't want to update to using V2.

2
Register Your Tastic Field Handler
Now that you've implemented your Tastic Field Handler for your Custom Stream, you need to register it in the Symfony service container and apply a tag to it so it knows what it is.

To do this, go to your service.xml in one of your Project bundles (if there isn't a bundle yet, you can create one using bin/console frontastic:create:bundle MyDecorators, for example) and then add the tag frontend.tasticFieldHandler to it in a similar way to the below:


<service id="Frontastic\Customer\StorefinderBundle\Domain\StreamHandler">
     <argument type="service" id="Frontastic\Customer\StorefinderBundle\Domain\StorefinderService" />

     <tag name="frontend.tasticFieldHandler"/>
</service>

In this example, a Tastic Field Handler is registered for implementing a Store Finder. The Field Handler implementation itself is rather slim and dispatches all work to a service. This gives you the possibility to unleash the full power of Frontastic, Symfony and your own micro-service APIs within a Field Handler.

3
Use the Stream in a Tastic
All you need to do now is add your Tastic Field Handler to a Tastic Schema. You must set the type to stream and then set the streamType to the type that you created in the first step, for example:
{
     "tasticType": "awesomeshop-store-finder",
     "name": "Storefinder",
     "category": "Awesome Shop",
     "icon": "store",
     "schema": [
	{
	     "name": "General",
	     "fields": [
		{
		     "label": "Stores",
		     "field": "stream",
		     "type": "stream",
		     "streamType": "awesomeshop-stores",
		     "default": null,
		     "required": false
		}
	     ]
	}
     ]
}

Accessing the Request

WARNING: We generally discourage accessing the request from a Service, however, until we can offer a better way to access such information it can be used as a workaround.

Since a Field Handler is just a normal Symfony Service you can get any service injected to dispatch work to. This also holds for the RequestStack which allows you to get hands on the currently processed request to access URL parameters and much more:

namespace Frontastic\Customer\StorefinderBundle\Domain;

use Symfony\Component\HttpFoundation\RequestStack;

use Frontastic\Catwalk\FrontendBundle\Domain\TasticFieldHandler;
use Frontastic\Catwalk\FrontendBundle\Domain\Stream;
use Frontastic\Catwalk\ApiCoreBundle\Domain\DataRepository;
use Frontastic\Catwalk\ApiCoreBundle\Domain\Context;
use Frontastic\Common\ProductApiBundle\Domain\ProductApi\Locale;

class StoresFieldHandler 
extends TasticFieldHandlerV2
{
     private $service;

     private $requestStack;

     public function __construct(StorefinderService $service, RequestStack $requestStack)
     {
	$this->service = $service;
	$this->requestStack = $requestStack;
     }

     public function getType(): string
     {
	return 'awesomeshop-stores';
     }

     public function handle(Context $context, Node $node, Page $page, $fieldValue)
     {
	$locationId = $this->requestStack->getCurrentRequest()->get('location', null);
	if (!$locationId) {
	     return null;
	}

	$locale = Locale::createFromPosix($context->locale);
	return $this->service->getStores($locale->territory, $locationId);
     }
}

In this example the RequestStack is asked for the current request to fetch the URL parameter location. If this isn't set, the Stream simply contains nothing. Otherwise the corresponding domain service is used to determine stores for the Stream.


‹ Back to Article List


Still need help? Contact Us Contact Us