Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

DP-36052: Fix - admin toolbar with view and translate showing on non-English pages #2760

Merged
merged 3 commits into from
Dec 9, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 41 additions & 0 deletions changelogs/DP-36052.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
#
# Write your changelog entry here. Every pull request must have a changelog yml file.
#
# Change types:
# #############################################################################
# You can use one of the following types:
# - Added: For new features.
# - Changed: For changes to existing functionality.
# - Deprecated: For soon-to-be removed features.
# - Removed: For removed features.
# - Fixed: For any bug fixes.
# - Security: In case of vulnerabilities.
#
# Format
# #############################################################################
# The format is crucial. Please follow the examples below. For reference, the requirements are:
# - All 3 parts are required and you must include "Type", "description" and "issue".
# - "Type" must be left aligned and followed by a colon.
# - "description" must be indented with 2 spaces followed by a colon
# - "issue" must be indented with 4 spaces followed by a colon.
# - "issue" is for the Jira ticket number only e.g. DP-1234
# - No extra spaces, indents, or blank lines are allowed.
#
# Example:
# #############################################################################
# Fixed:
# - description: Fixes scrolling on edit pages in Safari.
# issue: DP-13314
#
# You may add more than 1 description & issue for each type using the following format:
# Changed:
# - description: Automating the release branch.
# issue: DP-10166
# - description: Second change item that needs a description.
# issue: DP-19875
# - description: Third change item that needs a description along with an issue.
# issue: DP-19843
#
Fixed:
- description: Admin toolbar with view and translate showing on non-English pages.
issue: DP-36052
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use Drupal\Core\Access\AccessResult;
use Drupal\Core\Entity\EntityInterface;
use Drupal\Core\Session\AccountProxyInterface;
use Drupal\media\MediaStorage;
use Symfony\Component\DependencyInjection\ContainerInterface;

Expand All @@ -14,33 +15,64 @@
*/
class MediaTranslationsController extends TranslationsController {

/**
* The media storage service.
*
* @var \Drupal\media\MediaStorage
*/
protected $mediaStorage;

/**
* {@inheritdoc}
* The current user service.
*
* @var \Drupal\Core\Session\AccountProxyInterface
*/
public function __construct(MediaStorage $media_storage) {
$this->mediaStorage = $media_storage;
}
protected $currentUser;

/**
* {@inheritdoc}
* Constructs the MediaTranslationsController object.
*
* @param \Drupal\media\MediaStorage $media_storage
* The media storage service.
* @param \Drupal\Core\Session\AccountProxyInterface $current_user
* The current user service.
*/
public function access(EntityInterface $media) {
$languages = parent::getTranslationLanguages($media, $this->mediaStorage, $media->getEnglishFieldName());

return AccessResult::allowedIf(count($languages) > 1);
public function __construct(MediaStorage $media_storage, AccountProxyInterface $current_user) {
$this->mediaStorage = $media_storage;
$this->currentUser = $current_user;
}

/**
* {@inheritdoc}
*/
public static function create(ContainerInterface $container) {
return new static(
$container->get('entity_type.manager')->getStorage('media')
$container->get('entity_type.manager')->getStorage('media'),
$container->get('current_user')
);
}

/**
* Access check for the translations route.
*
* @param \Drupal\Core\Entity\EntityInterface $media
* The media entity.
*
* @return \Drupal\Core\Access\AccessResult
* The access result.
*/
public function access(EntityInterface $media) {
// Allow access only if the user is authenticated.
if ($this->currentUser->isAuthenticated()) {
// Check if the media entity has multiple translations.
$languages = parent::getTranslationLanguages($media, $this->mediaStorage, $media->getEnglishFieldName());
return AccessResult::allowedIf(count($languages) > 1);
}

// Deny access for anonymous users.
return AccessResult::forbidden();
}

/**
* {@inheritdoc}
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use Drupal\Core\Access\AccessResult;
use Drupal\Core\Entity\EntityInterface;
use Drupal\Core\Session\AccountProxyInterface;
use Drupal\node\NodeStorageInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;

Expand All @@ -14,33 +15,64 @@
*/
class NodeTranslationsController extends TranslationsController {

/**
* The node storage service.
*
* @var \Drupal\node\NodeStorageInterface
*/
protected $nodeStorage;

/**
* {@inheritdoc}
* The current user service.
*
* @var \Drupal\Core\Session\AccountProxyInterface
*/
public function __construct(NodeStorageInterface $node_storage) {
$this->nodeStorage = $node_storage;
}
protected $currentUser;

/**
* {@inheritdoc}
* Constructs the NodeTranslationsController object.
*
* @param \Drupal\node\NodeStorageInterface $node_storage
* The node storage service.
* @param \Drupal\Core\Session\AccountProxyInterface $current_user
* The current user service.
*/
public function access(EntityInterface $node) {
$languages = parent::getTranslationLanguages($node, $this->nodeStorage, $node->getEnglishFieldName());

return AccessResult::allowedIf(count($languages) > 1);
public function __construct(NodeStorageInterface $node_storage, AccountProxyInterface $current_user) {
$this->nodeStorage = $node_storage;
$this->currentUser = $current_user;
}

/**
* {@inheritdoc}
*/
public static function create(ContainerInterface $container) {
return new static(
$container->get('entity_type.manager')->getStorage('node')
$container->get('entity_type.manager')->getStorage('node'),
$container->get('current_user')
);
}

/**
* Access check for the translations route.
*
* @param \Drupal\Core\Entity\EntityInterface $node
* The node entity.
*
* @return \Drupal\Core\Access\AccessResult
* The access result.
*/
public function access(EntityInterface $node) {
// Allow access only if the user is authenticated.
if ($this->currentUser->isAuthenticated()) {
// Check if the node has multiple translations.
$languages = parent::getTranslationLanguages($node, $this->nodeStorage, $node->getEnglishFieldName());
return AccessResult::allowedIf(count($languages) > 1);
}

// Deny access for anonymous users.
return AccessResult::forbidden();
}

/**
* {@inheritdoc}
*/
Expand Down