PanelizerPanelsIPEController.php in Panelizer 8.3
File
src/Controller/PanelizerPanelsIPEController.php
View source
<?php
namespace Drupal\panelizer\Controller;
use Drupal\Component\Serialization\Json;
use Drupal\Core\Access\AccessResult;
use Drupal\Core\Controller\ControllerBase;
use Drupal\Core\Entity\FieldableEntityInterface;
use Drupal\Core\Session\AccountInterface;
use Drupal\panelizer\PanelizerInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpKernel\Exception\BadRequestHttpException;
use Symfony\Component\HttpKernel\Exception\HttpException;
class PanelizerPanelsIPEController extends ControllerBase {
protected $panelizer;
public function __construct(PanelizerInterface $panelizer) {
$this->panelizer = $panelizer;
}
public static function create(ContainerInterface $container) {
return new static($container
->get('panelizer'));
}
public function revertToDefault(FieldableEntityInterface $entity, $view_mode) {
$settings = $this->panelizer
->getPanelizerSettings($entity
->getEntityTypeId(), $entity
->bundle(), $view_mode);
$default = $settings['default'];
if ($entity
->hasField('panelizer') && $entity->panelizer
->first()) {
foreach ($entity->panelizer as $item) {
if ($item->view_mode == $view_mode && !empty($item->default)) {
$default = $item->default;
break;
}
}
}
if (empty($default)) {
throw new BadRequestHttpException("Default name to revert to must be passed!");
}
$this->panelizer
->setPanelsDisplay($entity, $view_mode, $default);
return new Response();
}
public function accessRevertToDefault(FieldableEntityInterface $entity, $view_mode, AccountInterface $account) {
return AccessResult::allowedIf($this->panelizer
->hasEntityPermission('revert to default', $entity, $view_mode, $account));
}
}