PhotosEditController.php in Album Photos 8.5
File
src/Controller/PhotosEditController.php
View source
<?php
namespace Drupal\photos\Controller;
use Drupal\Core\Url;
use Drupal\Core\Access\AccessResult;
use Drupal\Core\Controller\ControllerBase;
use Drupal\Core\Database\Connection;
use Drupal\Core\Extension\ModuleHandlerInterface;
use Drupal\Core\Form\FormBuilderInterface;
use Drupal\Core\Path\CurrentPathStack;
use Drupal\Core\Render\RendererInterface;
use Drupal\Core\Routing\RouteMatchInterface;
use Drupal\node\NodeInterface;
use Drupal\photos\PhotosAlbum;
use Drupal\photos\PhotosImageInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Symfony\Component\HttpFoundation\RedirectResponse;
use Symfony\Component\HttpFoundation\RequestStack;
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
class PhotosEditController extends ControllerBase {
protected $connection;
protected $currentPath;
protected $formBuilder;
protected $moduleHandler;
protected $renderer;
protected $requestStack;
protected $routeMatch;
public function __construct(Connection $connection, CurrentPathStack $current_path, FormBuilderInterface $form_builder, ModuleHandlerInterface $module_handler, RendererInterface $renderer, RequestStack $request_stack, RouteMatchInterface $route_match) {
$this->connection = $connection;
$this->currentPath = $current_path;
$this->formBuilder = $form_builder;
$this->moduleHandler = $module_handler;
$this->renderer = $renderer;
$this->requestStack = $request_stack;
$this->routeMatch = $route_match;
}
public static function create(ContainerInterface $container) {
return new static($container
->get('database'), $container
->get('path.current'), $container
->get('form_builder'), $container
->get('module_handler'), $container
->get('renderer'), $container
->get('request_stack'), $container
->get('current_route_match'));
}
public function access(NodeInterface $node, PhotosImageInterface $photos_image) {
if ($node && $photos_image) {
if ($node
->getType() == 'photos' && $node
->access('update')) {
return AccessResult::allowed();
}
else {
return AccessResult::forbidden();
}
}
else {
return AccessResult::neutral();
}
}
public function setAlbumCover(NodeInterface $node, PhotosImageInterface $photos_image) {
$nid = $node
->id();
$album_id = $this->connection
->query('SELECT album_id FROM {photos_image_field_data} WHERE id = :cover_id', [
':cover_id' => $photos_image
->id(),
])
->fetchField();
if ($album_id == $nid) {
$album = new PhotosAlbum($album_id);
$album
->setCover($photos_image
->id());
$get_destination = $this->requestStack
->getCurrentRequest()->query
->get('destination');
if ($get_destination) {
$goto = Url::fromUri('base:' . $get_destination)
->toString();
}
else {
$goto = $photos_image
->getAlbumUrl()
->toString();
}
return new RedirectResponse($goto);
}
else {
throw new NotFoundHttpException();
}
}
}