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
5f5fc595
Commit
5f5fc595
authored
2 months ago
by
Kostia Bohach
Browse files
Options
Downloads
Patches
Plain Diff
Issue
#3425638
: Conundrum when using Select 2
parent
8a9a68f8
No related branches found
Branches containing commit
Tags
1.1.4
1 merge request
!13
Issue #3425638: Conundrum when using Select 2
Pipeline
#353977
passed
2 months ago
Stage: build
Stage: validate
Changes
2
Pipelines
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
css/reference-field.css
+10
-2
10 additions, 2 deletions
css/reference-field.css
entity_reference_edit_link.module
+72
-14
72 additions, 14 deletions
entity_reference_edit_link.module
with
82 additions
and
16 deletions
css/reference-field.css
+
10
−
2
View file @
5f5fc595
/* stylelint-disable */
.field--widget-entity-reference-autocomplete
:has
(
.reference-edit-link
),
.field--widget-select2-entity-reference
:has
(
.reference-edit-link
)
{
display
:
flex
;
...
...
@@ -9,13 +10,20 @@
width
:
85%
;
}
.field--widget-entity-reference-autocomplete
.reference-edit-link
,
.field--widget-select2-entity-reference
:has
(
.reference-edit-link
)
.reference-edit-link
{
.field--widget-entity-reference-autocomplete
.reference-edit-link
{
height
:
100%
;
margin-top
:
1.6rem
;
margin-bottom
:
0
;
}
.field--widget-select2-entity-reference
:has
(
.reference-edit-link
)
.dropbutton-wrapper
{
margin-top
:
2.2rem
;
}
.field--widget-select2-entity-reference
:has
(
.reference-edit-link
)
.reference-edit-link
.default
a
{
pointer-events
:
none
;
}
.layout-region--node-secondary
.field--widget-entity-reference-autocomplete
{
align-items
:
center
;
}
...
...
This diff is collapsed.
Click to expand it.
entity_reference_edit_link.module
+
72
−
14
View file @
5f5fc595
...
...
@@ -5,6 +5,8 @@
* This is the module to create a drop-down menu for the core toolbar.
*/
use
Drupal\Core\Field\EntityReferenceFieldItemList
;
use
Drupal\Core\Field\WidgetBase
;
use
Drupal\Core\Form\FormStateInterface
;
use
Drupal\Core\Link
;
use
Drupal\Core\Render\Element
;
...
...
@@ -80,6 +82,8 @@ function entity_reference_edit_link_field_widget_info_alter(array &$info) {
/**
* Implements hook_field_widget_complete_form_alter().
*
* @throws \Drupal\Core\Entity\EntityMalformedException
*/
function
entity_reference_edit_link_field_widget_complete_form_alter
(
&
$field_widget_complete_form
,
FormStateInterface
$form_state
,
$context
)
{
$widget
=
$context
[
'widget'
]
??
NULL
;
...
...
@@ -88,19 +92,82 @@ function entity_reference_edit_link_field_widget_complete_form_alter(&$field_wid
if
(
!
$widget
||
$widget
->
getPluginId
()
!=
'select2_entity_reference'
)
{
return
;
}
$entity
=
!
empty
(
$context
[
'items'
])
?
$context
[
'items'
]
->
entity
:
NULL
;
$user
=
User
::
load
(
\Drupal
::
currentUser
()
->
id
());
if
(
empty
(
$entity
)
||
!
$entity
->
access
(
'update'
,
$user
))
{
return
;
}
$field_widget_complete_form
+=
[
'#attached'
=>
[
'library'
=>
[
'entity_reference_edit_link/reference.field'
],
],
];
$field_widget_complete_form
[
'widget'
]
=
[
'target_id'
=>
$field_widget_complete_form
[
'widget'
],
'_link'
=>
_entity_reference_edit_link_prepare_link
(
$widget
,
$context
[
'items'
],
$entity
),
];
}
/**
* Prepares a render array for the field edit link.
*
* @param \Drupal\Core\Field\WidgetBase $widget
* Field widget object.
* @param \Drupal\Core\Field\EntityReferenceFieldItemList $items
* Field items.
* @param \Drupal\Core\Entity\EntityInterface $entity
* Entity for the single value field case.
*
* @return array
* Returns render array with the link.
*
* @throws \Drupal\Core\Entity\EntityMalformedException
*/
function
_entity_reference_edit_link_prepare_link
(
WidgetBase
$widget
,
EntityReferenceFieldItemList
$items
,
mixed
$entity
)
{
// Check if the widget allow multiple values.
$multiple
=
$widget
->
getPluginDefinition
()[
'multiple_values'
]
??
FALSE
;
// Prepares container for the link.
$link
=
[
'#type'
=>
'container'
,
'#attributes'
=>
[
'class'
=>
[
'reference-edit-link-wrapper form-item'
],
],
'link'
=>
[
];
// Build multiple links if field widget allows multiple values.
if
(
$multiple
)
{
// Create default link value.
$links
[
'default'
]
=
[
'title'
=>
t
(
'Edit'
),
'url'
=>
Url
::
fromRoute
(
'<none>'
),
];
// Prepares link for the field values.
/** @var \Drupal\Core\Entity\EntityInterface $referencedEntity */
foreach
(
$items
->
referencedEntities
()
as
$referencedEntity
)
{
$links
[
$referencedEntity
->
id
()]
=
[
'title'
=>
$referencedEntity
->
label
(),
'url'
=>
$referencedEntity
->
toUrl
(
'edit-form'
),
'attributes'
=>
[
'target'
=>
'_blank'
,
],
];
}
// Render multiple links as a dropbutton.
$link
[
'link'
]
=
[
'#type'
=>
'dropbutton'
,
'#dropbutton_type'
=>
'small'
,
'#links'
=>
$links
,
'#attributes'
=>
[
'class'
=>
[
'reference-edit-link'
],
'target'
=>
'_blank'
,
],
];
}
// Handle single value field.
else
{
$link
[
'link'
]
=
[
'#type'
=>
'link'
,
'#title'
=>
t
(
'Edit'
),
'#url'
=>
$entity
->
toUrl
(
'edit-form'
),
...
...
@@ -108,19 +175,10 @@ function entity_reference_edit_link_field_widget_complete_form_alter(&$field_wid
'class'
=>
[
'button reference-edit-link'
],
'target'
=>
'_blank'
,
],
],
];
$field_widget_complete_form
+=
[
'#attached'
=>
[
'library'
=>
[
'entity_reference_edit_link/reference.field'
],
],
];
];
}
$field_widget_complete_form
[
'widget'
]
=
[
'target_id'
=>
$field_widget_complete_form
[
'widget'
],
'_link'
=>
$link
,
];
return
$link
;
}
/**
...
...
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