SiteSettingEntityAddController.php in Site Settings and Labels 8
File
src/Controller/SiteSettingEntityAddController.php
View source
<?php
namespace Drupal\site_settings\Controller;
use Drupal\Core\Controller\ControllerBase;
use Drupal\Core\Entity\EntityInterface;
use Drupal\Core\Entity\EntityStorageInterface;
use Drupal\Core\Url;
use Drupal\Core\Link;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Symfony\Component\HttpFoundation\Request;
class SiteSettingEntityAddController extends ControllerBase {
protected $storage;
protected $typeStorage;
public function __construct(EntityStorageInterface $storage, EntityStorageInterface $type_storage) {
$this->storage = $storage;
$this->typeStorage = $type_storage;
}
public static function create(ContainerInterface $container) {
$entity_type_manager = $container
->get('entity_type.manager');
return new static($entity_type_manager
->getStorage('site_setting_entity'), $entity_type_manager
->getStorage('site_setting_entity_type'));
}
public function add(Request $request) {
$types = $this->typeStorage
->loadMultiple();
if ($types && count($types) == 1) {
$type = reset($types);
return $this
->addForm($type, $request);
}
if (count($types) === 0) {
return [
'#markup' => $this
->t('You have not created any %bundle types yet. @link to add a new type.', [
'%bundle' => 'Site Setting',
'@link' => Link::fromTextAndUrl($this
->t('Go to the type creation page'), Url::fromRoute('entity.site_setting_entity_type.add_form'))
->toString(),
]),
];
}
return [
'#theme' => 'site_setting_entity_content_add_list',
'#content' => $types,
];
}
public function addForm(EntityInterface $site_setting_entity_type, Request $request) {
$entity = $this->storage
->create([
'type' => $site_setting_entity_type
->id(),
]);
return $this
->entityFormBuilder()
->getForm($entity);
}
public function getAddFormTitle(EntityInterface $site_setting_entity_type) {
return $this
->t('@label', [
'@label' => $site_setting_entity_type
->label(),
]);
}
}