PHP code example of adamwojs / ezplatform-location-reference

1. Go to this page and download the library: Download adamwojs/ezplatform-location-reference library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

adamwojs / ezplatform-location-reference example snippets




namespace App\Service; 

class FooService 
{
    /**
     * @var \AdamWojs\EzPlatformLocationReference\LocationReferenceResolverInterface  
     */
    private $locationReferenceResolver;

    public function __construct(LocationReferenceResolverInterface $locationReferenceResolver)
    {
        $this->locationReferenceResolver = $locationReferenceResolver;
    }

    public function foo(): void
    {
        $location = $this->locationReferenceResolver->resolve(
            'remote_id("babe4a915b1dd5d369e79adb9d6c0c6a")'
        );

        // ...
    }
}

 

interface LocationConfigResolverInterface
{
    public function getLocation(string $name, ?string $namespace = null, ?string $scope = null): Location;

    public function getLocationReference(string $name, ?string $namespace = null, ?string $scope = null): LocationReference;
}

 

class BarService 
{
    /**
     * @var \AdamWojs\EzPlatformLocationReference\ConfigResolver\LocationConfigResolverInterface  
     */
    private $locationConfigResolver;

    public function __construct(LocationConfigResolverInterface $locationConfigResolver)
    {
        $this->locationConfigResolver = $locationConfigResolver;
    }

    // ...

    public function foo(): void
    {
        // Get reference to location 
        $reference = $this->locationConfigResolver->getLocationReference('content.tree_root.location_id');
        
        // Resolve location reference 
        $location = $reference->getLocation();
        // Return null if location is not available (not found or unauthorized)  
        $location = $reference->getLocationOrNull();
        // Return $defaultLocation if location is not available (not found or unauthorized)
        $location = $reference->getLocationOrDefault($defaultLocation);
        
        // Get reference and immediately resolve
        $location = $this->locationConfigResolver->getLocation('fieldtypes.ezimageasset.parent_location');
    }
}