Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
P
prevnext-3433980
Manage
Activity
Members
Labels
Plan
Custom issue tracker
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
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
Issue forks
prevnext-3433980
Commits
62332ad1
Commit
62332ad1
authored
1 year ago
by
Viktor Holovachek
Committed by
Adriano Cori
1 year ago
Browse files
Options
Downloads
Patches
Plain Diff
Issue
#3398355
by AstonVictor: Prevnext links in block
3398355 - Add block plugin
parent
5f1b19be
No related branches found
Branches containing commit
Tags
2.0.8
Tags containing commit
No related merge requests found
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
src/Plugin/Block/PrevNextBlock.php
+131
-0
131 additions, 0 deletions
src/Plugin/Block/PrevNextBlock.php
with
131 additions
and
0 deletions
src/Plugin/Block/PrevNextBlock.php
0 → 100644
+
131
−
0
View file @
62332ad1
<?php
namespace
Drupal\prevnext\Plugin\Block
;
use
Drupal\Core\Block\BlockBase
;
use
Drupal\Core\Plugin\ContainerFactoryPluginInterface
;
use
Symfony\Component\DependencyInjection\ContainerInterface
;
use
Drupal\prevnext\PrevNextServiceInterface
;
use
Drupal\Core\Config\ConfigFactoryInterface
;
use
Drupal\Core\Url
;
/**
* Block with the Previous and Next links.
*
* @Block(
* id = "prevnext_block",
* admin_label = @Translation("PrevNext links"),
* category = @Translation("Other"),
* context_definitions = {
* "node" = @ContextDefinition("entity:node", required = TRUE, label = @Translation("Node"))
* }
* )
*/
class
PrevNextBlock
extends
BlockBase
implements
ContainerFactoryPluginInterface
{
/**
* Returns the prevnext.service service.
*
* @var \Drupal\prevnext\PrevNextServiceInterface
*/
protected
$prevnext
;
/**
* Returns the config.factory service.
*
* @var \Drupal\Core\Config\ConfigFactoryInterface
*/
protected
$configFactory
;
/**
* Constructs a PrevNextBlock block.
*
* @param array $configuration
* A configuration array containing information about the plugin instance.
* @param string $plugin_id
* The plugin ID for the plugin instance.
* @param mixed $plugin_definition
* The plugin implementation definition.
* @param \Drupal\prevnext\PrevNextServiceInterface $prevnext
* Interface for the main PrevNext service file.
* @param \Drupal\Core\Config\ConfigFactoryInterface $config_factory
* Defines the interface for a configuration object factory.
*/
public
function
__construct
(
array
$configuration
,
$plugin_id
,
$plugin_definition
,
PrevNextServiceInterface
$prevnext
,
ConfigFactoryInterface
$config_factory
)
{
parent
::
__construct
(
$configuration
,
$plugin_id
,
$plugin_definition
);
$this
->
prevnext
=
$prevnext
;
$this
->
configFactory
=
$config_factory
;
}
/**
* {@inheritdoc}
*/
public
static
function
create
(
ContainerInterface
$container
,
array
$configuration
,
$plugin_id
,
$plugin_definition
)
{
return
new
static
(
$configuration
,
$plugin_id
,
$plugin_definition
,
$container
->
get
(
'prevnext.service'
),
$container
->
get
(
'config.factory'
)
);
}
/**
* {@inheritdoc}
*/
public
function
build
()
{
$build
=
[];
/** @var \Drupal\node\NodeInterface $node */
$node
=
$this
->
getContextValue
(
'node'
);
if
(
!
empty
(
$node
->
in_preview
))
{
return
$build
;
}
$config
=
$this
->
configFactory
->
get
(
'prevnext.settings'
);
$node_types
=
$config
->
get
(
'prevnext_enabled_nodetypes'
);
if
(
empty
(
$node_types
[
$node
->
getType
()]))
{
return
$build
;
}
$previous_next
=
$this
->
prevnext
->
getPreviousNext
(
$node
);
$cache
=
[
'contexts'
=>
[
'url'
,
'user.permissions'
,
],
'tags'
=>
[
'config:prevnext.settings'
,
'node_list'
,
],
];
$build
[
'prevnext_previous'
]
=
[
'#theme'
=>
'prevnext'
,
'#direction'
=>
'previous'
,
'#text'
=>
$this
->
t
(
'Previous'
),
'#nid'
=>
$previous_next
[
'prev'
],
'#url'
=>
Url
::
fromUserInput
(
'/node/'
.
$previous_next
[
'prev'
])
->
toString
(),
'#void'
=>
empty
(
$previous_next
[
'prev'
]),
'#cache'
=>
$cache
,
];
$build
[
'prevnext_next'
]
=
[
'#theme'
=>
'prevnext'
,
'#direction'
=>
'next'
,
'#text'
=>
$this
->
t
(
'Next'
),
'#nid'
=>
$previous_next
[
'next'
],
'#url'
=>
Url
::
fromUserInput
(
'/node/'
.
$previous_next
[
'next'
])
->
toString
(),
'#void'
=>
empty
(
$previous_next
[
'next'
]),
'#cache'
=>
$cache
,
];
$build
[
'#cache'
][
'tags'
][]
=
'prevnext-'
.
$node
->
bundle
();
return
$build
;
}
}
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