Forest header image

Symfony Finland
Random things on PHP, Symfony and web development

How to hide a Location with the eZ Platform PHP API

This is a code example from the eZ Publish / eZ Platform API Cookbook on how to search and find Content using the eZ Publish content API in Symfony Controllers or Commands:

<?php
/**
 * File containing the HideLocationCommand class.
 *
 * @copyright Copyright (C) eZ Systems AS. All rights reserved.
 * @license For full copyright and license information view LICENSE file distributed with this source code.
 * @version //autogentag//
 */
namespace EzSystems\CookbookBundle\Command;

use Symfony\Bundle\FrameworkBundle\Command\ContainerAwareCommand,
    Symfony\Component\Console\Input\InputInterface,
    Symfony\Component\Console\Output\OutputInterface,
    Symfony\Component\Console\Input\InputArgument,
    Symfony\Component\Console\Input\InputOption;

/**
 * This command demonstrates how to hide and unhide a location (subtree)
 *
 * @author christianbacher
 *
 */
class HideLocationCommand extends ContainerAwareCommand
{
    protected function configure()
    {
        $this->setName( 'ezpublish:cookbook:add_location' )->setDefinition(
            array(
                new InputArgument( 'location_id', InputArgument::REQUIRED, 'An existing location id' ),
            )
        );
    }

    protected function execute( InputInterface $input, OutputInterface $output )
    {
        /** @var $repository \eZ\Publish\API\Repository\Repository */
        $repository = $this->getContainer()->get( 'ezpublish.api.repository' );
        $contentService = $repository->getContentService();
        $locationService = $repository->getLocationService();

        $repository->setCurrentUser( $repository->getUserService()->loadUser( 14 ) );

        // fetch the location argument
        $locationId = $input->getArgument( 'location_id' );

        try
        {
            $location = $contentService->loadContentInfo( $locationId );

            $hiddenLocation = $locationService->hideLocation( $location );
            $output->writeln( "<info>Location after hide:</info>" );
            print_r( $hiddenLocation );

            // unhide the now hidden location
            $unhiddenLocation = $locationService->unhideLocation( $hiddenLocation );
            $output->writeln( "\n<info>Location after unhide:</info>" );
            print_r( $unhiddenLocation );

        }
        catch ( \eZ\Publish\API\Repository\Exceptions\NotFoundException $e )
        {
            $output->writeln( "No location found with id $locationId" );
        }
    }
}

Learn more in the eZ Platform documentation or the eZ Platform Tips section on this site.


Written by Jani Tarvainen on Wednesday March 30, 2016
Permalink -

« How to delete a Subtree with the eZ Platform PHP API - How to list Sections with the eZ Platform PHP API »