Create a gist now

Instantly share code, notes, and snippets.

Embed
Local Tasks Deriver for Facebook Widgets
<?php
/**
* @file
* Contains \Drupal\facebook_widgets\Plugin\Derivative\DynamicLocalTasks.
*/
namespace Drupal\facebook_widgets\Plugin\Derivative;
use Drupal\Component\Plugin\Derivative\DeriverBase;
use Drupal\Core\Menu\LocalTaskManager;
use Drupal\Core\Plugin\Discovery\ContainerDeriverInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
/**
* Class DynamicLocalTasks
*
* @package Drupal\example\Plugin\Derivative
*/
class FacebookWidgetsLocalTasks extends DeriverBase implements ContainerDeriverInterface {
/**
* The base plugin ID.
*
* @var string
*/
private $basePluginId;
/**
* The local task manager
*
* @var array
*/
private $localTasks = array();
private $called = false;
public function __construct($basePluginId, LocalTaskManager $localTaskManager) {
$this->basePluginId = $basePluginId;
$this->localTasks = $localTaskManager->getDefinitions();
}
/**
* {@inheritdoc}
*/
public function getDerivativeDefinitions($base_plugin_definition) {
// Implement dynamic logic to provide values for the same keys as in example.links.task.yml.
$this->derivatives['facebook_widgets.list'] = $base_plugin_definition;
$tasksList = $this->localTasks;
foreach($tasksList as $task) {
if($task['parent_id'] == 'facebook_widgets.list') {
$this->derivatives['facebook_widgets.list']['route_name'] = 'facebook_comments.settings';
break;
}
}
return $this->derivatives;
}
/**
* {@inheritdoc}
*/
public static function create(ContainerInterface $container, $base_plugin_id) {
return new static(
$base_plugin_id,
$container->get('plugin.manager.menu.local_task')
);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment