66 lines
2.0 KiB
PHP
66 lines
2.0 KiB
PHP
<?php
|
|
|
|
namespace Drupal\opencase_no_cases\Plugin\Block;
|
|
|
|
use Drupal\Core\Block\BlockBase;
|
|
use Drupal\Core\Link;
|
|
use Drupal\Core\Url;
|
|
|
|
/**
|
|
* Provides a 'ContextualMenu' block.
|
|
*
|
|
* Displays links for adding activities against a person.
|
|
*
|
|
* @Block(
|
|
* id = "opencase_no_cases_contextual_menu",
|
|
* admin_label = @Translation("OpenCase Contextual Menu"),
|
|
* )
|
|
*/
|
|
class ContextualMenu extends BlockBase {
|
|
|
|
/**
|
|
* {@inheritdoc}
|
|
*/
|
|
public function build() {
|
|
$route_name = \Drupal::routeMatch()->getRouteName();
|
|
if ($route_name == 'entity.oc_actor.canonical' && \Drupal::routeMatch()->getParameter('oc_actor')->bundle() == 'client' ) {
|
|
;
|
|
$markup = $this->actorPage();
|
|
$build = [];
|
|
$build['contextual_menu'] = [
|
|
'#markup' => "<div id='opencase_contextual_menu'>$markup</div",
|
|
'#cache' => ['max-age' => 0]
|
|
];
|
|
return $build;
|
|
}
|
|
}
|
|
|
|
|
|
/**
|
|
* Contextual menu for Actor page
|
|
* - Links to add activities of various types
|
|
*/
|
|
private function actorPage() {
|
|
$actor_id = \Drupal::routeMatch()->getParameter('oc_actor')->id();
|
|
$current_path = \Drupal::service('path.current')->getPath();
|
|
return $this->generateLinksForAddingNewActivities("Add activity", ['actor_id' => $actor_id, 'destination' => $current_path]);
|
|
}
|
|
/**
|
|
* returns html markup.
|
|
*/
|
|
private function generateLinksForAddingNewActivities($title, $query = []) {
|
|
$title = t($title);
|
|
$markup = "<br /><p><strong>$title: </strong>";
|
|
$allActivityTypes = \Drupal::service('entity_type.bundle.info')->getBundleInfo('oc_activity');
|
|
foreach($allActivityTypes as $machine_name => $activityType) {
|
|
$label = $activityType['label'];
|
|
$url = \Drupal\Core\Url::fromRoute("entity.oc_activity.add_form", ['oc_activity_type' => $machine_name]);
|
|
$url->setOption('query', $query);
|
|
$link = \Drupal\Core\Link::fromTextAndUrl($label, $url)->toString();
|
|
$markup .= " $link";
|
|
}
|
|
$markup .= "</p>";
|
|
return "<div class='openactor_add_links'>$markup</div>";
|
|
}
|
|
}
|