Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
T
twig_backlink
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
twig_backlink
Commits
ac21e1bf
Commit
ac21e1bf
authored
1 year ago
by
Roshni Bedmutha
Committed by
Jacques R. Blier
1 year ago
Browse files
Options
Downloads
Patches
Plain Diff
Resolve
#3372626
"Fix the warningserrors"
parent
535e8df0
No related branches found
Branches containing commit
Tags
1.0.0-alpha3
Tags containing commit
1 merge request
!2
Resolve #3372626 "Fix the warningserrors"
Changes
3
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
README.md
+0
-1
0 additions, 1 deletion
README.md
src/Form/TwigBacklinkConfigurationForm.php
+36
-2
36 additions, 2 deletions
src/Form/TwigBacklinkConfigurationForm.php
src/TwigBacklinkTwigExtension.php
+45
-17
45 additions, 17 deletions
src/TwigBacklinkTwigExtension.php
with
81 additions
and
20 deletions
README.md
+
0
−
1
View file @
ac21e1bf
...
...
@@ -11,4 +11,3 @@ This extension will automatically build a list of parents when an Entity Referen
## Requirements
-
An Entity Reference field added to a node.
-
Access to a node twig file that the themer can edit.
This diff is collapsed.
Click to expand it.
src/Form/TwigBacklinkConfigurationForm.php
+
36
−
2
View file @
ac21e1bf
...
...
@@ -4,12 +4,46 @@ namespace Drupal\twig_backlink\Form;
use
Drupal\Core\Form\ConfigFormBase
;
use
Drupal\Core\Form\FormStateInterface
;
use
Drupal\Core\Messenger\MessengerInterface
;
use
Symfony\Component\DependencyInjection\ContainerInterface
;
/**
* Defines a form that configures the label used by the Twig Backlink module.
*/
class
TwigBacklinkConfigurationForm
extends
ConfigFormBase
{
/**
* The Messenger service.
*
* @var \Drupal\Core\Messenger\MessengerInterface
*/
protected
$messenger
;
/**
* Constructs a new twig backline class object.
*
* @param \Drupal\Core\Messenger\MessengerInterface $messenger
* The Messenger service.
*/
public
function
__construct
(
MessengerInterface
$messenger
)
{
$this
->
messenger
=
$messenger
;
}
/**
* Creates an instance of this class.
*
* @param \Symfony\Component\DependencyInjection\ContainerInterface $container
* The container to resolve services.
*
* @return static
* The instance of this class.
*/
public
static
function
create
(
ContainerInterface
$container
)
{
return
new
static
(
$container
->
get
(
'messenger'
)
);
}
/**
* {@inheritdoc}
*/
...
...
@@ -39,7 +73,7 @@ class TwigBacklinkConfigurationForm extends ConfigFormBase {
'#type'
=>
'textfield'
,
'#title'
=>
$this
->
t
(
'Label'
),
'#default_value'
=>
$default_value
,
//'#required' => TRUE,
//
'#required' => TRUE,
];
return
parent
::
buildForm
(
$form
,
$form_state
);
}
...
...
@@ -52,7 +86,7 @@ class TwigBacklinkConfigurationForm extends ConfigFormBase {
$this
->
config
(
'twig_backlink.settings'
)
->
set
(
'label'
,
$values
[
'label'
])
->
save
();
\Drupal
::
messenger
()
->
addStatus
(
'Twig Backlink label saved.'
);
$this
->
messenger
->
addStatus
(
'Twig Backlink label saved.'
);
}
}
This diff is collapsed.
Click to expand it.
src/TwigBacklinkTwigExtension.php
+
45
−
17
View file @
ac21e1bf
<?php
//#Drush auto-generate all file
// #Drush auto-generate all file
namespace
Drupal\twig_backlink
;
use
Collator
;
use
Drupal\Core\Config\ConfigFactoryInterface
;
use
Drupal\Core\Entity\EntityTypeManagerInterface
;
use
Drupal\Core\Language\LanguageManagerInterface
;
use
Drupal\Core\Routing\RouteMatchInterface
;
use
Drupal\Core\Url
;
use
Drupal\node\Entity\Nod
e
;
use
Symfony\Component\DependencyInjection\ContainerInterfac
e
;
use
Twig\Extension\AbstractExtension
;
use
Twig\TwigFunction
;
/**
* This twig extension adds a new twig_backlink function to build a list of
* links of the parent entities.
* This twig extension adds a new twig_backlink function to build a list of links of the parent entities.
*/
class
TwigBacklinkTwigExtension
extends
AbstractExtension
{
/**
* The Language Manager service.
*
* @var \Drupal\Core\Language\LanguageManagerInterface
*/
protected
$languageManager
;
/**
* The current route match.
...
...
@@ -42,6 +47,8 @@ class TwigBacklinkTwigExtension extends AbstractExtension {
/**
* Constructs a new TwigBacklinkTwigExtension object.
*
* @param \Drupal\Core\Language\LanguageManagerInterface $language_manager
* The language manager.
* @param \Drupal\Core\Routing\RouteMatchInterface $route_match
* The current route match.
* @param \Drupal\Core\Config\ConfigFactoryInterface $config_factory
...
...
@@ -49,10 +56,30 @@ class TwigBacklinkTwigExtension extends AbstractExtension {
* @param \Drupal\Core\Entity\EntityTypeManagerInterface $entity_type_manager
* The entity type manager.
*/
public
function
__construct
(
RouteMatchInterface
$route_match
,
ConfigFactoryInterface
$config_factory
,
EntityTypeManagerInterface
$entity_type_manager
)
{
public
function
__construct
(
LanguageManagerInterface
$language_manager
,
RouteMatchInterface
$route_match
,
ConfigFactoryInterface
$config_factory
,
EntityTypeManagerInterface
$entity_type_manager
)
{
$this
->
routeMatch
=
$route_match
;
$this
->
configFactory
=
$config_factory
;
$this
->
entityTypeManager
=
$entity_type_manager
;
$this
->
languageManager
=
$language_manager
;
}
/**
* Creates an instance of this class.
*
* @param \Symfony\Component\DependencyInjection\ContainerInterface $container
* The container to resolve services.
*
* @return static
* The instance of this class.
*/
public
static
function
create
(
ContainerInterface
$container
)
{
return
new
static
(
$container
->
get
(
'language_manager'
),
$container
->
get
(
'current_route_match'
),
$container
->
get
(
'entity_type.manager'
),
$container
->
get
(
'config.factory'
)
);
}
/**
...
...
@@ -82,7 +109,7 @@ class TwigBacklinkTwigExtension extends AbstractExtension {
*/
public
function
twigBacklink
(
$field_name
):
?array
{
$current_nid
=
$this
->
routeMatch
->
getParameter
(
'node'
);
$langcode
=
\Drupal
::
languageManager
()
->
getCurrentLanguage
()
->
getId
();
$langcode
=
$this
->
languageManager
->
getCurrentLanguage
()
->
getId
();
if
(
!
is_null
(
$current_nid
))
{
$nid
=
$current_nid
->
nid
->
value
;
}
...
...
@@ -102,14 +129,14 @@ class TwigBacklinkTwigExtension extends AbstractExtension {
if
(
$nids
)
{
foreach
(
$nids
as
$nid
)
{
$node
=
Node
::
load
(
$nid
);
$node
=
$this
->
entityTypeManager
->
getStorage
(
'node'
)
->
load
(
$nid
);
$translation
=
$node
->
getTranslation
(
$langcode
);
$options
=
[
'attributes'
=>
[
'class'
=>
'backlink-link'
]];
$links
[
'backlink_links'
][]
=
[
'#title'
=>
(
$translation
->
label
()
?
$translation
->
label
()
:
$node
->
label
()),
'#type'
=>
'link'
,
'#url'
=>
Url
::
fromRoute
(
'entity.node.canonical'
,
[
'node'
=>
$nid
],
$options
)
[
'node'
=>
$nid
],
$options
)
,
];
}
}
...
...
@@ -137,25 +164,25 @@ class TwigBacklinkTwigExtension extends AbstractExtension {
$query
=
$this
->
entityTypeManager
->
getStorage
(
'node'
)
->
getQuery
();
return
$query
// Referenced ID
// Referenced ID
.
->
accessCheck
(
FALSE
)
->
condition
(
$field_name
,
$nid
)
->
condition
(
'status'
,
1
)
->
execute
();
}
}
}
/**
* Local sort function.
*
* @return array
*/
function
_twig_backlink_array_sort
(
$array
,
$on
,
$order
=
SORT_ASC
):
array
{
$new_array
=
array
()
;
$sortable_array
=
array
()
;
$lang_code
=
\Drupal
::
languageManager
()
->
getCurrentLanguage
()
->
getId
();
$collator
=
new
Collator
(
$lang_code
);
function
_twig_backlink_array_sort
(
$array
,
$on
,
$order
=
SORT_ASC
):
array
{
$new_array
=
[]
;
$sortable_array
=
[]
;
$lang_code
=
$this
->
languageManager
->
getCurrentLanguage
()
->
getId
();
$collator
=
new
\
Collator
(
$lang_code
);
if
(
count
(
$array
)
>
0
)
{
foreach
(
$array
as
$k
=>
$v
)
{
...
...
@@ -165,7 +192,8 @@ function _twig_backlink_array_sort($array, $on, $order=SORT_ASC): array {
$sortable_array
[
$k
]
=
$v2
;
}
}
}
else
{
}
else
{
$sortable_array
[
$k
]
=
$v
;
}
}
...
...
This diff is collapsed.
Click to expand it.
Jacques R. Blier
@xmacinfo
mentioned in commit
72cdb925
·
1 year ago
mentioned in commit
72cdb925
mentioned in commit 72cdb925459ebebf8d212acf44fab66b4039a5c1
Toggle commit list
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