-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathNodeBlock.php
50 lines (44 loc) · 1.17 KB
/
NodeBlock.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
/**
* @file
* Contains \Drupal\my_module\Plugin\Block\NodeMenuBlock.
*/
namespace Drupal\my_module\Plugin\Block;
use Drupal\Core\Block\BlockBase;
use Drupal\Core\Link;
use Drupal\Core\Url;
/**
* @Block(
* id = "node_menu_block",
* admin_label = @Translation("Node Menu Block"),
* category = @Translation("My Group"),
* context = {
* "node" = @ContextDefinition(
* "entity:node",
* label = @Translation("Current Node")
* )
* }
* )
*/
class NodeMenuBlock extends BlockBase {
/**
* {@inheritdoc}
*/
public function build() {
$node = $this->getContextValue('node');
$nid_fld = $node->nid->getValue();
$nid = $nid_fld[0]['value'];
$markup = '';
$links = ['entity.node.edit_form' => 'Edit', 'entity.node.delete_form' => 'Delete', ];
foreach($links as $rout=>$text) {
$url = Url::fromRoute($rout, array('node' => $nid));
$link = Link::fromTextAndUrl(t($text), $url)->toRenderable();
$link['#attributes'] = array('class' => array('button', 'button-action'));
$markup .= render($link).' ';
}
$block = [
'#type' => 'markup',
'#markup' => $markup,
];
return $block;
}
}