class SocialAlbumController in Open Social 10.1.x
Same name and namespace in other branches
- 10.3.x modules/social_features/social_album/src/Controller/SocialAlbumController.php \Drupal\social_album\Controller\SocialAlbumController
- 10.0.x modules/social_features/social_album/src/Controller/SocialAlbumController.php \Drupal\social_album\Controller\SocialAlbumController
- 10.2.x modules/social_features/social_album/src/Controller/SocialAlbumController.php \Drupal\social_album\Controller\SocialAlbumController
Returns responses for Album routes.
@package Drupal\social_album\Controller
Hierarchy
- class \Drupal\Core\Controller\ControllerBase implements ContainerInjectionInterface uses LoggerChannelTrait, MessengerTrait, RedirectDestinationTrait, StringTranslationTrait
- class \Drupal\social_album\Controller\SocialAlbumController
Expanded class hierarchy of SocialAlbumController
2 files declare their use of SocialAlbumController
- SocialAlbumGroupAccess.php in modules/
social_features/ social_album/ src/ Plugin/ views/ access/ SocialAlbumGroupAccess.php - SocialAlbumUserAccess.php in modules/
social_features/ social_album/ src/ Plugin/ views/ access/ SocialAlbumUserAccess.php
File
- modules/
social_features/ social_album/ src/ Controller/ SocialAlbumController.php, line 24
Namespace
Drupal\social_album\ControllerView source
class SocialAlbumController extends ControllerBase {
/**
* The current active database's master connection.
*
* @var \Drupal\Core\Database\Connection
*/
protected $database;
/**
* SocialAlbumController constructor.
*
* @param \Drupal\Core\StringTranslation\TranslationInterface $translation
* The string translation service.
* @param \Drupal\Core\Database\Connection $database
* The current active database's master connection.
* @param \Drupal\Core\Entity\EntityTypeManagerInterface $entity_type_manager
* The entity type manager.
* @param \Drupal\Core\Entity\EntityFormBuilderInterface $entity_form_builder
* The entity form builder.
* @param \Drupal\Core\Session\AccountInterface $current_user
* The current user.
* @param \Drupal\Core\Config\ConfigFactoryInterface $config_factory
* The configuration factory service.
*/
public function __construct(TranslationInterface $translation, Connection $database, EntityTypeManagerInterface $entity_type_manager, EntityFormBuilderInterface $entity_form_builder, AccountInterface $current_user, ConfigFactoryInterface $config_factory) {
$this
->setStringTranslation($translation);
$this->database = $database;
$this->entityTypeManager = $entity_type_manager;
$this->entityFormBuilder = $entity_form_builder;
$this->currentUser = $current_user;
$this->configFactory = $config_factory;
}
/**
* {@inheritdoc}
*/
public static function create(ContainerInterface $container) {
return new static($container
->get('string_translation'), $container
->get('database'), $container
->get('entity_type.manager'), $container
->get('entity.form_builder'), $container
->get('current_user'), $container
->get('config.factory'));
}
/**
* Provides a generic title callback for the first post of the album.
*
* @param \Drupal\node\NodeInterface $node
* The node object.
*
* @return \Drupal\Core\StringTranslation\TranslatableMarkup
* The title to page of the post.
*/
public function title(NodeInterface $node) {
return $this
->t('Add images to album @name', [
'@name' => $node
->label(),
]);
}
/**
* Provides a page with images slider.
*
* @param \Drupal\node\NodeInterface $node
* The node object.
* @param \Drupal\social_post\Entity\PostInterface $post
* The post entity object.
* @param int $fid
* The file entity ID.
*
* @return array
* The renderable array.
*/
public function viewImage(NodeInterface $node, PostInterface $post, $fid) {
$query = $this->database
->select('post__field_post_image', 'i')
->fields('i', [
'field_post_image_target_id',
]);
$query
->innerJoin('post__field_album', 'a', 'a.entity_id = i.entity_id');
$query
->condition('a.field_album_target_id', $node
->id());
$query
->innerJoin('post_field_data', 'p', 'p.id = a.entity_id');
$query
->fields('p', [
'id',
]);
$query
->orderBy('p.created');
$query
->orderBy('i.delta');
$items = [
FALSE => [],
TRUE => [],
];
$found = FALSE;
/** @var \Drupal\file\FileStorageInterface $storage */
$storage = $this
->entityTypeManager()
->getStorage('file');
foreach ($query
->execute()
->fetchAllKeyed() as $file_id => $post_id) {
if (!$found && $file_id == $fid) {
$found = TRUE;
}
/** @var \Drupal\file\FileInterface $file */
$file = $storage
->load($file_id);
$items[$found][] = [
'url' => Url::fromUri(file_create_url($file
->getFileUri()))
->setAbsolute()
->toString(),
'pid' => $post_id,
];
}
return [
'#theme' => 'social_album_post',
'#items' => array_merge($items[TRUE], $items[FALSE]),
'#album' => $node
->label(),
];
}
/**
* Provides a page with a form for deleting image from post and post view.
*
* @param \Drupal\node\NodeInterface $node
* The node object.
* @param \Drupal\social_post\Entity\PostInterface $post
* The post entity object.
* @param int $fid
* The file entity ID.
*
* @return array
* The renderable array.
*/
public function deleteImage(NodeInterface $node, PostInterface $post, $fid) {
return [
'form' => $this
->entityFormBuilder()
->getForm($post, 'delete_image', [
'fid' => $fid,
]),
'view' => $this
->entityTypeManager()
->getViewBuilder('post')
->view($post, 'featured'),
];
}
/**
* Set the current group as the default value of the group field.
*
* @param \Drupal\group\Entity\GroupInterface $group
* The group object.
*
* @return array
* The renderable array.
*/
public function add(GroupInterface $group) {
$node = $this
->entityTypeManager()
->getStorage('node')
->create([
'type' => 'album',
'groups' => $group,
]);
return $this
->entityFormBuilder()
->getForm($node);
}
/**
* Checks access to the form of a post which will be linked to the album.
*
* @param \Drupal\node\NodeInterface $node
* The node object.
*
* @return bool
* TRUE if it's an album node.
*/
protected function checkAlbumAccess(NodeInterface $node) {
return $node
->bundle() === 'album';
}
/**
* Checks access to the page for adding an image to an album.
*
* @param \Drupal\node\NodeInterface $node
* The node entity object.
*
* @return \Drupal\Core\Access\AccessResultInterface
* The access result.
*/
public function checkAddImageAccess(NodeInterface $node) {
if ($this
->checkAlbumAccess($node)) {
$account = $this
->currentUser();
if ($node
->getOwnerId() === $account
->id()) {
return AccessResult::allowed();
}
elseif (!$node->field_album_creators
->isEmpty() && $node->field_album_creators->value) {
/** @var \Drupal\group\Entity\Storage\GroupContentStorageInterface $storage */
$storage = $this
->entityTypeManager()
->getStorage('group_content');
if ($group_content = $storage
->loadByEntity($node)) {
/** @var \Drupal\group\Entity\GroupInterface $group */
$group = reset($group_content)
->getGroup();
return AccessResult::allowedIf($group
->getMember($account) !== FALSE);
}
}
}
return AccessResult::forbidden();
}
/**
* Checks access to the page for viewing the image from the post.
*
* @param \Drupal\node\NodeInterface $node
* The node entity object.
* @param \Drupal\social_post\Entity\PostInterface $post
* The post entity object.
* @param int $fid
* The file entity ID.
* @param string $operation
* (optional) The operation to be performed. Defaults to view.
*
* @return \Drupal\Core\Access\AccessResultInterface
* The access result.
*/
public function checkViewImageAccess(NodeInterface $node, PostInterface $post, $fid, $operation = 'view') {
if ($this
->checkAlbumAccess($node) && $post
->access($operation) && $post
->bundle() === 'photo' && !$post->field_album
->isEmpty() && $post->field_album->target_id === $node
->id() && !$post->field_post_image
->isEmpty()) {
foreach ($post->field_post_image
->getValue() as $item) {
if ($item['target_id'] === $fid) {
return AccessResult::allowed();
}
}
}
return AccessResult::forbidden();
}
/**
* Checks access to the page for deleting the image from the post.
*
* @param \Drupal\node\NodeInterface $node
* The node entity object.
* @param \Drupal\social_post\Entity\PostInterface $post
* The post entity object.
* @param int $fid
* The file entity ID.
*
* @return \Drupal\Core\Access\AccessResultInterface
* The access result.
*/
public function checkDeleteImageAccess(NodeInterface $node, PostInterface $post, $fid) {
$access = $this
->checkViewImageAccess($node, $post, $fid, 'delete');
if ($access
->isAllowed()) {
$access = $access
->andIf(AccessResult::allowedIf($post
->getOwnerId() === $this
->currentUser()
->id()));
}
return $access;
}
/**
* Checks access to the user albums page.
*
* @return \Drupal\Core\Access\AccessResultInterface
* The access result.
*/
public function checkUserAlbumsAccess() {
$status = $this
->config('social_album.settings')
->get('status');
return AccessResult::allowedIf(!empty($status));
}
/**
* Checks access to create an album in a group.
*
* @param \Drupal\group\Entity\GroupInterface $group
* The group object.
*
* @return \Drupal\Core\Access\AccessResultInterface
* The access result.
*/
protected function checkGroupAccess(GroupInterface $group) {
$is_allow = $group
->getGroupType()
->hasContentPlugin('group_node:album');
return AccessResult::allowedIf($is_allow);
}
/**
* Checks access to the group albums page.
*
* @param \Drupal\group\Entity\GroupInterface $group
* The group object.
*
* @return \Drupal\Core\Access\AccessResultInterface
* The access result.
*/
public function checkGroupAlbumsAccess(GroupInterface $group) {
$access = $this
->checkUserAlbumsAccess();
return $access
->isForbidden() ? $access : $this
->checkGroupAccess($group);
}
/**
* Checks access to the group album creation page.
*
* @param \Drupal\group\Entity\GroupInterface $group
* The group object.
*
* @return \Drupal\Core\Access\AccessResultInterface
* The access result.
*/
public function checkGroupAlbumAccess(GroupInterface $group) {
$access = $this
->checkGroupAccess($group);
if ($access
->isAllowed()) {
return $group
->getGroupType()
->getContentPlugin('group_node:album')
->createEntityAccess($group, $this
->currentUser())
->andIf($this
->checkUserAlbumsAccess());
}
return $access;
}
}
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 entity type manager. | |
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. | |
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. | |
SocialAlbumController:: |
protected | property | The current active database's master connection. | |
SocialAlbumController:: |
public | function | Set the current group as the default value of the group field. | |
SocialAlbumController:: |
public | function | Checks access to the page for adding an image to an album. | |
SocialAlbumController:: |
protected | function | Checks access to the form of a post which will be linked to the album. | |
SocialAlbumController:: |
public | function | Checks access to the page for deleting the image from the post. | |
SocialAlbumController:: |
protected | function | Checks access to create an album in a group. | |
SocialAlbumController:: |
public | function | Checks access to the group album creation page. | |
SocialAlbumController:: |
public | function | Checks access to the group albums page. | |
SocialAlbumController:: |
public | function | Checks access to the user albums page. | |
SocialAlbumController:: |
public | function | Checks access to the page for viewing the image from the post. | |
SocialAlbumController:: |
public static | function |
Instantiates a new instance of this class. Overrides ControllerBase:: |
|
SocialAlbumController:: |
public | function | Provides a page with a form for deleting image from post and post view. | |
SocialAlbumController:: |
public | function | Provides a generic title callback for the first post of the album. | |
SocialAlbumController:: |
public | function | Provides a page with images slider. | |
SocialAlbumController:: |
public | function | SocialAlbumController constructor. | |
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. |