This repository has been archived on 2022-07-12. You can view files and clone it, but cannot push or open issues or pull requests.
opencase/src/Utils.php

36 lines
1.1 KiB
PHP
Raw Normal View History

2018-04-30 14:49:44 +00:00
<?php
namespace Drupal\opencase;
/**
* Shared functions for the opencase module
*
*/
class Utils {
/**
* Generates a set of links for adding different types of a base entity
*
* $baseEntityType the type of entity to generate the links for (it will generate one for each bundle of the base type)
2018-05-03 11:35:35 +00:00
* $title the title to be placed above the set of links)
* $query optionally append a query string to the links (key => value format
2018-04-30 14:49:44 +00:00
*
* returns html markup.
*/
2018-05-03 11:35:35 +00:00
public static function generateAddLinks($baseEntityType, $title, $query = []) {
2018-04-30 14:49:44 +00:00
$bundles = \Drupal::service('entity_type.bundle.info')->getBundleInfo($baseEntityType);
2018-05-03 11:35:35 +00:00
$title = t($title);
$markup = "<h1>$title: </h1>";
2018-04-30 14:49:44 +00:00
foreach($bundles as $bundle_id => $bundle) {
2018-05-03 11:35:35 +00:00
$label = t($bundle['label']);
2018-04-30 14:49:44 +00:00
$url = \Drupal\Core\Url::fromRoute("entity.$baseEntityType.add_form", [$baseEntityType . '_type' => $bundle_id]);
$url->setOption('query', $query);
2018-05-03 11:35:35 +00:00
$link = \Drupal\Core\Link::fromTextAndUrl($label, $url)->toString();
2018-04-30 14:49:44 +00:00
$markup .= "<p>$link</p>";
}
return "<div class='opencase_add_links'>$markup</div>";
}
}