Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
E
entity_reference_edit_link
Manage
Activity
Members
Labels
Plan
Wiki
Custom issue tracker
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Locked files
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Model registry
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Code review analytics
Insights
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Terms and privacy
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
project
entity_reference_edit_link
Commits
dc9ce4e7
Commit
dc9ce4e7
authored
10 months ago
by
Nidhi Patadia
Committed by
Kostia Bohach
10 months ago
Browse files
Options
Downloads
Patches
Plain Diff
#3428569
Check permissions before render an edit link for the node and content type
parent
898fee35
No related branches found
No related tags found
1 merge request
!10
#3428569 Check permissions before render an edit link for the node and content type
Pipeline
#146681
passed
10 months ago
Stage: build
Stage: validate
Changes
2
Pipelines
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
entity_reference_edit_link.module
+20
-7
20 additions, 7 deletions
entity_reference_edit_link.module
src/Plugin/Field/FieldWidget/EntityReferenceEditLinkAutocompleteWidget.php
+32
-0
32 additions, 0 deletions
...FieldWidget/EntityReferenceEditLinkAutocompleteWidget.php
with
52 additions
and
7 deletions
entity_reference_edit_link.module
+
20
−
7
View file @
dc9ce4e7
...
...
@@ -12,6 +12,7 @@ use Drupal\Core\Url;
use
Drupal\entity_reference_edit_link
\Plugin\Field\FieldWidget\EntityReferenceEditLinkAutocompleteTagsWidget
;
use
Drupal\entity_reference_edit_link
\Plugin\Field\FieldWidget\EntityReferenceEditLinkAutocompleteWidget
;
use
Drupal\node\NodeForm
;
use
Drupal\user\Entity\User
;
/**
* Implements hook_preprocess_HOOK().
...
...
@@ -88,7 +89,9 @@ function entity_reference_edit_link_field_widget_complete_form_alter(&$field_wid
return
;
}
if
(
empty
(
$items
=
$context
[
'items'
])
||
empty
(
$entity
=
$items
->
entity
))
{
$entity
=
!
empty
(
$context
[
'items'
])
?
$context
[
'items'
]
->
entity
:
NULL
;
$user
=
User
::
load
(
\Drupal
::
currentUser
()
->
id
());
if
(
empty
(
$entity
)
||
!
$entity
->
access
(
'update'
,
$user
))
{
return
;
}
...
...
@@ -134,8 +137,8 @@ function entity_reference_edit_link_form_alter(&$form, FormStateInterface $form_
if
(
!
$node
)
{
return
;
}
if
(
entity_reference_edit_link_allowed_content_type
(
$node
))
{
$form
[
'#title'
]
=
entity_reference_edit_link_build_entity_type_link
(
$node
);
if
(
_
entity_reference_edit_link_allowed_content_type
(
$node
)
&&
_entity_reference_edit_link_check_permissions
()
)
{
$form
[
'#title'
]
=
_
entity_reference_edit_link_build_entity_type_link
(
$node
);
}
}
...
...
@@ -149,8 +152,8 @@ function entity_reference_edit_link_preprocess_page_title(&$variables) {
if
(
!
$node
||
\Drupal
::
routeMatch
()
->
getRouteName
()
!=
'entity.node.edit_form'
)
{
return
;
}
if
(
entity_reference_edit_link_allowed_content_type
(
$node
))
{
$variables
[
'#title'
]
=
entity_reference_edit_link_build_entity_type_link
(
$node
);
if
(
_
entity_reference_edit_link_allowed_content_type
(
$node
)
&&
_entity_reference_edit_link_check_permissions
()
)
{
$variables
[
'#title'
]
=
_
entity_reference_edit_link_build_entity_type_link
(
$node
);
}
}
...
...
@@ -170,7 +173,7 @@ function entity_reference_edit_link_theme_registry_alter(&$theme_registry) {
/**
* Implements hook_entity().
*/
function
entity_reference_edit_link_build_entity_type_link
(
$node
)
{
function
_
entity_reference_edit_link_build_entity_type_link
(
$node
)
{
$url
=
Url
::
fromRoute
(
"entity.
{
$node
->
getEntityTypeId
()
}
.field_ui_fields"
,
[
'node_type'
=>
$node
->
bundle
(),
],
...
...
@@ -186,7 +189,17 @@ function entity_reference_edit_link_build_entity_type_link($node) {
/**
* Implements custom function to check allowed content types.
*/
function
entity_reference_edit_link_allowed_content_type
(
$node
)
{
function
_
entity_reference_edit_link_allowed_content_type
(
$node
)
{
return
!
empty
(
$allowedContentTypes
=
\Drupal
::
config
(
'edit_link.config'
)
->
get
(
'content_types'
))
&&
in_array
(
$node
->
bundle
(),
$allowedContentTypes
);
}
/**
* Checks user permissions.
*
* @return bool
* Returns TRUE if current user has specific permission.
*/
function
_entity_reference_edit_link_check_permissions
()
{
return
\Drupal
::
currentUser
()
->
hasPermission
(
'administer node fields'
);
}
This diff is collapsed.
Click to expand it.
src/Plugin/Field/FieldWidget/EntityReferenceEditLinkAutocompleteWidget.php
+
32
−
0
View file @
dc9ce4e7
...
...
@@ -5,12 +5,38 @@ namespace Drupal\entity_reference_edit_link\Plugin\Field\FieldWidget;
use
Drupal\Core\Field\FieldItemListInterface
;
use
Drupal\Core\Field\Plugin\Field\FieldWidget\EntityReferenceAutocompleteWidget
;
use
Drupal\Core\Form\FormStateInterface
;
use
Symfony\Component\DependencyInjection\ContainerInterface
;
/**
* Class to alter original entity reference autocomplete tags widget.
*/
class
EntityReferenceEditLinkAutocompleteWidget
extends
EntityReferenceAutocompleteWidget
{
/**
* The current user.
*
* @var \Drupal\Core\Session\AccountInterface
*/
protected
$currentUser
;
/**
* The user storage.
*
* @var \Drupal\user\UserStorageInterface
*/
protected
$userStorage
;
/**
* {@inheritdoc}
*/
public
static
function
create
(
ContainerInterface
$container
,
array
$configuration
,
$plugin_id
,
$plugin_definition
)
{
$instance
=
parent
::
create
(
$container
,
$configuration
,
$plugin_id
,
$plugin_definition
);
$instance
->
currentUser
=
$container
->
get
(
'current_user'
);
$instance
->
userStorage
=
$container
->
get
(
'entity_type.manager'
)
->
getStorage
(
'user'
);
return
$instance
;
}
/**
* {@inheritdoc}
*/
...
...
@@ -22,6 +48,12 @@ class EntityReferenceEditLinkAutocompleteWidget extends EntityReferenceAutocompl
return
parent
::
formElement
(
$items
,
$delta
,
$element
,
$form
,
$form_state
);
}
$user
=
$this
->
userStorage
->
load
(
$this
->
currentUser
->
id
());
/** @var \Drupal\Core\Entity\EntityInterface $referencedEntity */
if
(
!
$referencedEntity
->
access
(
'update'
,
$user
))
{
return
parent
::
formElement
(
$items
,
$delta
,
$element
,
$form
,
$form_state
);
}
$element
+=
[
'#attached'
=>
[
'library'
=>
[
'entity_reference_edit_link/reference.field'
],
...
...
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment