class PhotosAlbumController in Album Photos 6.0.x
Same name and namespace in other branches
- 8.5 src/Controller/PhotosAlbumController.php \Drupal\photos\Controller\PhotosAlbumController
- 8.4 src/Controller/PhotosAlbumController.php \Drupal\photos\Controller\PhotosAlbumController
Album view controller.
Hierarchy
- class \Drupal\Core\Controller\ControllerBase implements ContainerInjectionInterface uses LoggerChannelTrait, MessengerTrait, RedirectDestinationTrait, StringTranslationTrait
- class \Drupal\photos\Controller\PhotosAlbumController
Expanded class hierarchy of PhotosAlbumController
File
- src/
Controller/ PhotosAlbumController.php, line 29
Namespace
Drupal\photos\ControllerView source
class PhotosAlbumController extends ControllerBase {
/**
* The database connection.
*
* @var \Drupal\Core\Database\Connection
*/
protected $connection;
/**
* The date formatter service.
*
* @var \Drupal\Core\Datetime\DateFormatterInterface
*/
protected $dateFormatter;
/**
* The entity manager.
*
* @var \Drupal\Core\Entity\EntityTypeManagerInterface
*/
protected $entityTypeManager;
/**
* The image factory.
*
* @var \Drupal\Core\Image\ImageFactory
*/
protected $imageFactory;
/**
* The library discovery service.
*
* @var \Drupal\Core\Asset\LibraryDiscoveryInterface
*/
protected $libraryDiscovery;
/**
* The renderer.
*
* @var \Drupal\Core\Render\RendererInterface
*/
protected $renderer;
/**
* The current request stack.
*
* @var \Symfony\Component\HttpFoundation\RequestStack
*/
protected $requestStack;
/**
* The current route match.
*
* @var \Drupal\Core\Routing\RouteMatchInterface
*/
protected $routeMatch;
/**
* Constructor.
*
* @param \Drupal\Core\Database\Connection $connection
* The database connection.
* @param \Drupal\Core\Datetime\DateFormatterInterface $date_formatter
* The date formatter service.
* @param \Drupal\Core\Entity\EntityTypeManagerInterface $entity_manager
* The entity manager service.
* @param \Drupal\Core\Image\ImageFactory $image_factory
* The image factory.
* @param \Drupal\Core\Asset\LibraryDiscoveryInterface $library_discovery
* The library discovery service.
* @param \Drupal\Core\Render\RendererInterface $renderer
* The renderer.
* @param \Symfony\Component\HttpFoundation\RequestStack $request_stack
* The current request stack.
* @param \Drupal\Core\Routing\RouteMatchInterface $route_match
* The current route match.
*/
public function __construct(Connection $connection, DateFormatterInterface $date_formatter, EntityTypeManagerInterface $entity_manager, ImageFactory $image_factory, LibraryDiscoveryInterface $library_discovery, RendererInterface $renderer, RequestStack $request_stack, RouteMatchInterface $route_match) {
$this->connection = $connection;
$this->dateFormatter = $date_formatter;
$this->entityTypeManager = $entity_manager;
$this->imageFactory = $image_factory;
$this->libraryDiscovery = $library_discovery;
$this->renderer = $renderer;
$this->requestStack = $request_stack;
$this->routeMatch = $route_match;
}
/**
* {@inheritdoc}
*/
public static function create(ContainerInterface $container) {
return new static($container
->get('database'), $container
->get('date.formatter'), $container
->get('entity_type.manager'), $container
->get('image.factory'), $container
->get('library.discovery'), $container
->get('renderer'), $container
->get('request_stack'), $container
->get('current_route_match'));
}
/**
* Set page title.
*/
public function getTitle() {
// Get node object.
$node = $this->routeMatch
->getParameter('node');
$title = 'Album: ' . $node
->getTitle();
return $title;
}
/**
* A custom access check.
*
* @param \Drupal\Core\Session\AccountInterface $account
* The current user.
* @param \Drupal\node\NodeInterface $node
* The album node entity.
*
* @return \Drupal\Core\Access\AccessResult
* The access result.
*/
public function access(AccountInterface $account, NodeInterface $node) {
if (!$node) {
// Not found.
throw new NotFoundHttpException();
}
// Check access.
if ($account
->hasPermission('view photo') && $node
->access('view')) {
// Allow access.
return AccessResult::allowed();
}
else {
return AccessResult::forbidden();
}
}
/**
* Returns an overview of recent albums and photos.
*
* @return array
* A render array.
*/
public function albumView() {
// @todo move to theme function and deprecate this in favor of default view.
$config = $this
->config('photos.settings');
// Get node object.
$album = [];
$node = $this->routeMatch
->getParameter('node');
$nid = $node
->id();
// Get order or set default order.
$order = explode('|', isset($node->album['imageorder']) ? $node->album['imageorder'] : $config
->get('photos_display_imageorder'));
$order = PhotosAlbum::orderValueChange($order[0], $order[1]);
$limit = isset($node->album['viewpager']) ? $node->album['viewpager'] : $config
->get('photos_display_viewpager');
$get_field = $this->requestStack
->getCurrentRequest()->query
->get('field');
$get_sort = $this->requestStack
->getCurrentRequest()->query
->get('sort');
$column = $get_field ? Html::escape($get_field) : '';
$sort = isset($get_sort) ? Html::escape($get_sort) : '';
$term = PhotosAlbum::orderValue($column, $sort, $limit, $order);
// Album image's query.
// @todo move to PhotosAlbum()->getImages().
// @todo entity query?
$query = $this->connection
->select('photos_image_field_data', 'p')
->extend('Drupal\\Core\\Database\\Query\\PagerSelectExtender');
// @todo p.fid will fail.
$query
->join('users_field_data', 'u', 'u.uid = p.uid');
$query
->fields('p', [
'id',
])
->condition('p.album_id', $nid)
->limit($term['limit']);
if ($term['order']['column'] != 'p.id') {
$query
->orderBy('p.id', 'DESC');
}
$results = $query
->execute();
// Check comment settings.
$com = $config
->get('photos_comment');
// Check node access.
$edit = $node
->access('update');
$del = $node
->access('delete');
$style_name = isset($node->album['list_imagesize']) ? $node->album['list_imagesize'] : $config
->get('photos_display_list_imagesize');
// Necessary when upgrading from D6 to D7.
// @todo fix in migration if needed?
$image_styles = image_style_options(FALSE);
if (!isset($image_styles[$style_name])) {
$style_name = $config
->get('photos_display_list_imagesize');
}
// Process images.
// @todo load multiple list view.
// @todo use view for default album view.
foreach ($results as $result) {
// Load photos image.
try {
$photosImage = $this->entityTypeManager
->getStorage('photos_image')
->load($result->id);
} catch (InvalidPluginDefinitionException $e) {
watchdog_exception('photos', $e);
throw new NotFoundHttpException();
} catch (PluginNotFoundException $e) {
watchdog_exception('photos', $e);
throw new NotFoundHttpException();
}
$render_photos_image = $this->entityTypeManager
->getViewBuilder('photos_image')
->view($photosImage, 'full');
$album['view'][] = $render_photos_image;
}
if (isset($album['view'][0])) {
$album['access']['edit'] = $edit;
// Node edit link.
$url = Url::fromUri('base:node/' . $nid . '/edit');
$album['node_edit_url'] = Link::fromTextAndUrl($this
->t('Album settings'), $url);
// Image management link.
$url = Url::fromUri('base:node/' . $nid . '/photos');
$album['image_management_url'] = Link::fromTextAndUrl($this
->t('Upload photos'), $url);
// Album URL.
$album['album_url'] = Url::fromUri('base:photos/' . $nid)
->toString();
$album['links'] = PhotosAlbum::orderLinks('photos/' . $nid, 0, 0, 1);
$cover_style_name = $config
->get('photos_cover_imagesize');
// Album cover view.
if (isset($node->album['cover_id'])) {
$coverId = $node->album['cover_id'];
$photos_album = new PhotosAlbum($node
->id());
$album['cover'] = $photos_album
->getCover($coverId);
}
else {
// @todo is this needed?
$image_info = $this->imageFactory
->get($node->album['cover']['uri']);
$title = $node
->getTitle();
$album_cover_array = [
'#theme' => 'image_style',
'#style_name' => $cover_style_name,
'#uri' => $node->album['cover']['uri'],
'#width' => $image_info
->getWidth(),
'#height' => $image_info
->getHeight(),
'#alt' => $title,
'#title' => $title,
'#cache' => [
'tags' => [
'photos:album:' . $nid,
'node:' . $nid,
],
],
];
$album['cover'] = $album_cover_array;
}
$album['pager'] = [
'#type' => 'pager',
];
// Build album view.
$album_view_array = [
'#theme' => 'photos_album_view',
'#album' => $album,
'#node' => $node,
'#cache' => [
'tags' => [
'photos:album:' . $nid,
'node:' . $nid,
],
],
];
$content = $album_view_array;
}
else {
$content = [
'#markup' => $this
->t('Album is empty'),
'#cache' => [
'tags' => [
'photos:album:' . $nid,
'node:' . $nid,
],
],
];
}
return $content;
}
/**
* Returns content for recent albums.
*
* @return array
* An array containing markup for the page content.
*/
public function listView() {
// @todo convert this to a theme function for photos_album_photo_list field.
$build = [
'#cache' => [
'tags' => [],
],
];
// @todo a lot of duplicate code can be consolidated in these controllers.
$query = $this->connection
->select('node', 'n')
->extend('Drupal\\Core\\Database\\Query\\PagerSelectExtender');
$query
->join('photos_album', 'p', 'p.album_id = n.nid');
$query
->fields('n', [
'nid',
]);
$query
->orderBy('n.nid', 'DESC');
$query
->limit(10);
$query
->addTag('node_access');
$results = $query
->execute();
$build['photos_albums'] = [];
// Check the setting for album list node view mode.
$view_mode = $this
->config('photos.settings')
->get('view_mode_album_list_page');
if (!$view_mode) {
$view_mode = 'teaser';
}
foreach ($results as $result) {
$node = $this->entityTypeManager
->getStorage('node')
->load($result->nid);
$node_view = $this->entityTypeManager
->getViewBuilder('node')
->view($node, $view_mode);
$build['photos_albums'][] = $node_view;
$build['#cache']['tags'][] = 'node:' . $node
->id();
$build['#cache']['tags'][] = 'photos:album:' . $node
->id();
}
if (!empty($build['photos_albums'])) {
$build['#cache']['tags'][] = 'node_list';
$build['pager'] = [
'#type' => 'pager',
];
}
else {
$build['photos_albums'][] = [
'#markup' => $this
->t('No albums have been created yet.'),
];
}
return $build;
}
}
Members
Name | Modifiers | Type | Description | Overrides |
---|---|---|---|---|
ControllerBase:: |
protected | property | The configuration factory. | |
ControllerBase:: |
protected | property | The current user service. | 1 |
ControllerBase:: |
protected | property | The entity form builder. | |
ControllerBase:: |
protected | property | The form builder. | 2 |
ControllerBase:: |
protected | property | The key-value storage. | 1 |
ControllerBase:: |
protected | property | The language manager. | 1 |
ControllerBase:: |
protected | property | The module handler. | 2 |
ControllerBase:: |
protected | property | The state service. | |
ControllerBase:: |
protected | function | Returns the requested cache bin. | |
ControllerBase:: |
protected | function | Retrieves a configuration object. | |
ControllerBase:: |
private | function | Returns the service container. | |
ControllerBase:: |
protected | function | Returns the current user. | 1 |
ControllerBase:: |
protected | function | Retrieves the entity form builder. | |
ControllerBase:: |
protected | function | Retrieves the entity type manager. | |
ControllerBase:: |
protected | function | Returns the form builder service. | 2 |
ControllerBase:: |
protected | function | Returns a key/value storage collection. | 1 |
ControllerBase:: |
protected | function | Returns the language manager service. | 1 |
ControllerBase:: |
protected | function | Returns the module handler. | 2 |
ControllerBase:: |
protected | function | Returns a redirect response object for the specified route. | |
ControllerBase:: |
protected | function | Returns the state storage service. | |
LoggerChannelTrait:: |
protected | property | The logger channel factory service. | |
LoggerChannelTrait:: |
protected | function | Gets the logger for a specific channel. | |
LoggerChannelTrait:: |
public | function | Injects the logger channel factory. | |
MessengerTrait:: |
protected | property | The messenger. | 27 |
MessengerTrait:: |
public | function | Gets the messenger. | 27 |
MessengerTrait:: |
public | function | Sets the messenger. | |
PhotosAlbumController:: |
protected | property | The database connection. | |
PhotosAlbumController:: |
protected | property | The date formatter service. | |
PhotosAlbumController:: |
protected | property |
The entity manager. Overrides ControllerBase:: |
|
PhotosAlbumController:: |
protected | property | The image factory. | |
PhotosAlbumController:: |
protected | property | The library discovery service. | |
PhotosAlbumController:: |
protected | property | The renderer. | |
PhotosAlbumController:: |
protected | property | The current request stack. | |
PhotosAlbumController:: |
protected | property | The current route match. | |
PhotosAlbumController:: |
public | function | A custom access check. | |
PhotosAlbumController:: |
public | function | Returns an overview of recent albums and photos. | |
PhotosAlbumController:: |
public static | function |
Instantiates a new instance of this class. Overrides ControllerBase:: |
|
PhotosAlbumController:: |
public | function | Set page title. | |
PhotosAlbumController:: |
public | function | Returns content for recent albums. | |
PhotosAlbumController:: |
public | function | Constructor. | |
RedirectDestinationTrait:: |
protected | property | The redirect destination service. | 1 |
RedirectDestinationTrait:: |
protected | function | Prepares a 'destination' URL query parameter for use with \Drupal\Core\Url. | |
RedirectDestinationTrait:: |
protected | function | Returns the redirect destination service. | |
RedirectDestinationTrait:: |
public | function | Sets the redirect destination service. | |
StringTranslationTrait:: |
protected | property | The string translation service. | 4 |
StringTranslationTrait:: |
protected | function | Formats a string containing a count of items. | |
StringTranslationTrait:: |
protected | function | Returns the number of plurals supported by a given language. | |
StringTranslationTrait:: |
protected | function | Gets the string translation service. | |
StringTranslationTrait:: |
public | function | Sets the string translation service to use. | 2 |
StringTranslationTrait:: |
protected | function | Translates a string to the current language or to a given language. |