Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
J
jsonapi_menu
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
jsonapi_menu
Commits
3e71ca50
Commit
3e71ca50
authored
1 year ago
by
Carlos Mella
Committed by
Jaime Herencia
1 year ago
Browse files
Options
Downloads
Patches
Plain Diff
Get translations for the menu items and add dependency injection.
parent
a5f1436a
Branches
Branches containing commit
Tags
1.0.0-beta3
Tags containing commit
1 merge request
!1
Get translations for the menu items and add dependency injection.
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
src/Plugin/MenuItemsFormat/JsonApiMenuItemsFormat.php
+51
-8
51 additions, 8 deletions
src/Plugin/MenuItemsFormat/JsonApiMenuItemsFormat.php
src/Plugin/MenuItemsFormat/NestedMenuItemsFormat.php
+49
-9
49 additions, 9 deletions
src/Plugin/MenuItemsFormat/NestedMenuItemsFormat.php
with
100 additions
and
17 deletions
src/Plugin/MenuItemsFormat/JsonApiMenuItemsFormat.php
+
51
−
8
View file @
3e71ca50
...
...
@@ -3,12 +3,20 @@
namespace
Drupal\jsonapi_menu\Plugin\MenuItemsFormat
;
use
Drupal\Core\Cache\CacheableMetadata
;
use
Drupal\Core\Entity\EntityRepositoryInterface
;
use
Drupal\Core\Entity\EntityTypeManagerInterface
;
use
Drupal\Core\Extension\ModuleHandlerInterface
;
use
Drupal\Core\Menu\MenuLinkInterface
;
use
Drupal\Core\Menu\MenuLinkTreeInterface
;
use
Drupal\Core\Menu\MenuTreeParameters
;
use
Drupal\Core\Plugin\ContainerFactoryPluginInterface
;
use
Drupal\jsonapi\JsonApiResource\ResourceObject
;
use
Drupal\jsonapi\JsonApiResource\ResourceObjectData
;
use
Drupal\jsonapi\ResourceType\ResourceTypeRepositoryInterface
;
use
Drupal\jsonapi_menu
\Plugin\MenuItemsFormatBase
;
use
Drupal\system\MenuInterface
;
use
Symfony\Component\DependencyInjection\ContainerInterface
;
use
Symfony\Component\Serializer\SerializerInterface
;
/**
* Plugin implementation of the 'json_api' format.
...
...
@@ -17,7 +25,7 @@ use Drupal\system\MenuInterface;
* id = "json_api"
* )
*/
class
JsonApiMenuItemsFormat
extends
MenuItemsFormatBase
{
class
JsonApiMenuItemsFormat
extends
MenuItemsFormatBase
implements
ContainerFactoryPluginInterface
{
/**
* Drupal\Core\Menu\MenuLinkTreeInterface definition.
*
...
...
@@ -53,16 +61,50 @@ class JsonApiMenuItemsFormat extends MenuItemsFormatBase {
*/
protected
$resourceTypeRepository
;
public
function
__construct
(
array
$configuration
,
$plugin_id
,
$plugin_definition
)
{
parent
::
__construct
(
$configuration
,
$plugin_id
,
$plugin_definition
);
/**
* The entity respository.
*
* @var \Drupal\Core\Entity\EntityRepositoryInterface
*/
protected
$entityRepository
;
$this
->
menuLinkTree
=
\Drupal
::
service
(
'menu.link_tree'
);
$this
->
moduleHandler
=
\Drupal
::
service
(
'module_handler'
);
$this
->
entityTypeManager
=
\Drupal
::
service
(
'entity_type.manager'
);
$this
->
serializer
=
\Drupal
::
service
(
'jsonapi.serializer'
);
$this
->
resourceTypeRepository
=
\Drupal
::
service
(
'jsonapi.resource_type.repository'
);
/**
* @param array $configuration
* @param $plugin_id
* @param $plugin_definition
* @param \Drupal\Core\Menu\MenuLinkTreeInterface $menuLinkTree
* @param \Drupal\Core\Extension\ModuleHandlerInterface $moduleHandler
* @param \Drupal\Core\Entity\EntityTypeManagerInterface $entityTypeManager
* @param \Symfony\Component\Serializer\SerializerInterface $serializer
* @param \Drupal\jsonapi\ResourceType\ResourceTypeRepositoryInterface $resourceTypeRepository
* @param \Drupal\Core\Entity\EntityRepositoryInterface $entityRepository
*/
public
function
__construct
(
array
$configuration
,
$plugin_id
,
$plugin_definition
,
MenuLinkTreeInterface
$menuLinkTree
,
ModuleHandlerInterface
$moduleHandler
,
EntityTypeManagerInterface
$entityTypeManager
,
SerializerInterface
$serializer
,
ResourceTypeRepositoryInterface
$resourceTypeRepository
,
EntityRepositoryInterface
$entityRepository
)
{
parent
::
__construct
(
$configuration
,
$plugin_id
,
$plugin_definition
);
$this
->
menuLinkTree
=
$menuLinkTree
;
$this
->
moduleHandler
=
$moduleHandler
;
$this
->
entityTypeManager
=
$entityTypeManager
;
$this
->
serializer
=
$serializer
;
$this
->
resourceTypeRepository
=
$resourceTypeRepository
;
$this
->
entityRepository
=
$entityRepository
;
}
/**
* {@inheritDoc}
*/
public
static
function
create
(
ContainerInterface
$container
,
array
$configuration
,
$plugin_id
,
$plugin_definition
)
{
return
new
static
(
$configuration
,
$plugin_id
,
$plugin_definition
,
$container
->
get
(
'menu.link_tree'
),
$container
->
get
(
'module_handler'
),
$container
->
get
(
'entity_type.manager'
),
$container
->
get
(
'jsonapi.serializer'
),
$container
->
get
(
'jsonapi.resource_type.repository'
),
$container
->
get
(
'entity.repository'
),
);
}
protected
function
getMenuItemResourceType
(
MenuLinkInterface
$menuLink
)
{
if
(
$this
->
moduleHandler
->
moduleExists
(
'menu_item_extras'
))
{
...
...
@@ -119,6 +161,7 @@ class JsonApiMenuItemsFormat extends MenuItemsFormatBase {
->
loadByProperties
([
'uuid'
=>
$menuLinkEntityId
])
;
$menuLinkEntity
=
reset
(
$query
);
$menuLinkEntity
=
$this
->
entityRepository
->
getTranslationFromContext
(
$menuLinkEntity
);
$items
[]
=
ResourceObject
::
createFromEntity
(
$resourceType
,
$menuLinkEntity
);
if
(
$menuTreeLink
->
subtree
)
{
...
...
This diff is collapsed.
Click to expand it.
src/Plugin/MenuItemsFormat/NestedMenuItemsFormat.php
+
49
−
9
View file @
3e71ca50
...
...
@@ -3,15 +3,24 @@
namespace
Drupal\jsonapi_menu\Plugin\MenuItemsFormat
;
use
Drupal\Core\Cache\CacheableMetadata
;
use
Drupal\Core\Entity\EntityFieldManagerInterface
;
use
Drupal\Core\Entity\EntityRepositoryInterface
;
use
Drupal\Core\Entity\EntityTypeManagerInterface
;
use
Drupal\Core\Extension\ModuleHandlerInterface
;
use
Drupal\Core\Field\FieldConfigInterface
;
use
Drupal\Core\GeneratedUrl
;
use
Drupal\Core\Menu\MenuLinkInterface
;
use
Drupal\Core\Menu\MenuLinkTreeElement
;
use
Drupal\Core\Menu\MenuLinkTreeInterface
;
use
Drupal\Core\Menu\MenuTreeParameters
;
use
Drupal\Core\Plugin\ContainerFactoryPluginInterface
;
use
Drupal\jsonapi\JsonApiResource\ResourceObject
;
use
Drupal\jsonapi\ResourceType\ResourceTypeRepositoryInterface
;
use
Drupal\jsonapi_menu
\Plugin\MenuItemsFormatBase
;
use
Drupal\menu_link_content
\MenuLinkContentInterface
;
use
Drupal\system\MenuInterface
;
use
Symfony\Component\DependencyInjection\ContainerInterface
;
use
Symfony\Component\Serializer\SerializerInterface
;
/**
* Plugin implementation of the 'nested' format.
...
...
@@ -20,7 +29,8 @@ use Drupal\system\MenuInterface;
* id = "nested"
* )
*/
class
NestedMenuItemsFormat
extends
MenuItemsFormatBase
{
class
NestedMenuItemsFormat
extends
MenuItemsFormatBase
implements
ContainerFactoryPluginInterface
{
/**
* Drupal\Core\Menu\MenuLinkTreeInterface definition.
*
...
...
@@ -72,16 +82,45 @@ class NestedMenuItemsFormat extends MenuItemsFormatBase {
static
array
$resourceTypeCache
=
[];
public
function
__construct
(
array
$configuration
,
$plugin_id
,
$plugin_definition
)
{
/**
* @param array $configuration
* @param $plugin_id
* @param $plugin_definition
* @param \Drupal\Core\Menu\MenuLinkTreeInterface $menuLinkTree
* @param \Drupal\Core\Extension\ModuleHandlerInterface $moduleHandler
* @param \Drupal\Core\Entity\EntityTypeManagerInterface $entityTypeManager
* @param \Symfony\Component\Serializer\SerializerInterface $serializer
* @param \Drupal\jsonapi\ResourceType\ResourceTypeRepositoryInterface $resourceTypeRepository
* @param \Drupal\Core\Entity\EntityRepositoryInterface $entityRepository
* @param \Drupal\Core\Entity\EntityFieldManagerInterface $entityFieldManager
*/
public
function
__construct
(
array
$configuration
,
$plugin_id
,
$plugin_definition
,
MenuLinkTreeInterface
$menuLinkTree
,
ModuleHandlerInterface
$moduleHandler
,
EntityTypeManagerInterface
$entityTypeManager
,
SerializerInterface
$serializer
,
ResourceTypeRepositoryInterface
$resourceTypeRepository
,
EntityRepositoryInterface
$entityRepository
,
EntityFieldManagerInterface
$entityFieldManager
)
{
parent
::
__construct
(
$configuration
,
$plugin_id
,
$plugin_definition
);
$this
->
menuLinkTree
=
$menuLinkTree
;
$this
->
moduleHandler
=
$moduleHandler
;
$this
->
entityTypeManager
=
$entityTypeManager
;
$this
->
serializer
=
$serializer
;
$this
->
resourceTypeRepository
=
$resourceTypeRepository
;
$this
->
entityRepository
=
$entityRepository
;
$this
->
entityFieldManager
=
$entityFieldManager
;
}
$this
->
menuLinkTree
=
\Drupal
::
service
(
'menu.link_tree'
);
$this
->
moduleHandler
=
\Drupal
::
service
(
'module_handler'
);
$this
->
entityTypeManager
=
\Drupal
::
service
(
'entity_type.manager'
);
$this
->
serializer
=
\Drupal
::
service
(
'jsonapi.serializer'
);
$this
->
resourceTypeRepository
=
\Drupal
::
service
(
'jsonapi.resource_type.repository'
);
$this
->
entityRepository
=
\Drupal
::
service
(
'entity.repository'
);
$this
->
entityFieldManager
=
\Drupal
::
service
(
'entity_field.manager'
);
/**
* {@inheritDoc}
*/
public
static
function
create
(
ContainerInterface
$container
,
array
$configuration
,
$plugin_id
,
$plugin_definition
)
{
return
new
static
(
$configuration
,
$plugin_id
,
$plugin_definition
,
$container
->
get
(
'menu.link_tree'
),
$container
->
get
(
'module_handler'
),
$container
->
get
(
'entity_type.manager'
),
$container
->
get
(
'jsonapi.serializer'
),
$container
->
get
(
'jsonapi.resource_type.repository'
),
$container
->
get
(
'entity.repository'
),
$container
->
get
(
'entity_field.manager'
),
);
}
/**
...
...
@@ -159,6 +198,7 @@ class NestedMenuItemsFormat extends MenuItemsFormatBase {
if
(
$plugin
===
'menu_link_content'
)
{
/* @var $menuLinkContentEntity MenuLinkContentInterface */
$menuLinkContentEntity
=
$this
->
entityRepository
->
loadEntityByUuid
(
'menu_link_content'
,
$menuLinkEntityId
);
$menuLinkContentEntity
=
$this
->
entityRepository
->
getTranslationFromContext
(
$menuLinkContentEntity
);
$this
->
addMenuLinkContentFieldValues
(
$menuLink
,
$menuLinkContentEntity
,
$data
);
$data
[
'uri'
]
=
$menuLinkContentEntity
->
link
->
uri
;
...
...
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