EckContentController.php in Entity Construction Kit (ECK) 8
File
src/Controller/EckContentController.php
View source
<?php
namespace Drupal\eck\Controller;
use Drupal\Core\Controller\ControllerBase;
use Drupal\Core\DependencyInjection\ContainerInjectionInterface;
use Drupal\Core\Render\RendererInterface;
use Drupal\eck\EckEntityTypeInterface;
use Drupal\eck\Entity\EckEntityBundle;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
class EckContentController extends ControllerBase implements ContainerInjectionInterface {
protected $renderer;
public function __construct(RendererInterface $renderer) {
$this->renderer = $renderer;
}
public static function create(ContainerInterface $container) {
return new static($container
->get('renderer'));
}
public function addPage(EckEntityTypeInterface $eck_entity_type) {
$content = [];
$bundleStorage = $this
->getBundleStorage($eck_entity_type);
foreach ($bundleStorage
->loadMultiple() as $bundle) {
if ($this
->entityTypeManager()
->getAccessControlHandler($eck_entity_type
->id())
->createAccess($bundle->type)) {
$content[$bundle->type] = $bundle;
}
}
return [
'#theme' => 'eck_content_add_list',
'#content' => $content,
'#entity_type' => [
'id' => $eck_entity_type
->id(),
'label' => $eck_entity_type
->label(),
],
];
}
public function add(EckEntityTypeInterface $eck_entity_type, $eck_entity_bundle) {
$bundleStorage = $this
->getBundleStorage($eck_entity_type);
if (!$bundleStorage
->load($eck_entity_bundle)) {
throw new NotFoundHttpException($this
->t('Bundle %bundle does not exist', [
'%bundle' => $eck_entity_bundle,
]));
}
$entityStorage = $this
->entityTypeManager()
->getStorage($eck_entity_type
->id());
$entity = $entityStorage
->create([
'type' => $eck_entity_bundle,
]);
return $this
->entityFormBuilder()
->getForm($entity);
}
public function addPageTitle(EckEntityTypeInterface $eck_entity_type) {
return $this
->t('Add %label content', [
'%label' => $eck_entity_type
->label(),
]);
}
public function addContentPageTitle($eck_entity_bundle) {
$eck_entity_bundle = EckEntityBundle::load($eck_entity_bundle);
return $this
->t('Add %label content', [
'%label' => $eck_entity_bundle
->get('name'),
]);
}
private function getBundleStorage(EckEntityTypeInterface $eck_entity_type) {
$entityTypeBundle = "{$eck_entity_type->id()}_type";
$bundleStorage = $this
->entityTypeManager()
->getStorage($entityTypeBundle);
return $bundleStorage;
}
}