123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596 |
- <?php
- namespace Drupal\statusmessage\Controller;
- use Drupal\Core\Controller\ControllerBase;
- use Drupal\Core\Entity\EntityInterface;
- use Drupal\Core\Entity\EntityTypeManagerInterface;
- use Drupal\Core\Entity\EntityStorageInterface;
- use Drupal\Core\Url;
- use Symfony\Component\DependencyInjection\ContainerInterface;
- use Symfony\Component\HttpFoundation\Request;
- class StatusAddController extends ControllerBase {
- 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('status'),
- $entity_type_manager->getStorage('status_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 array(
- '#markup' => $this->t('You have not created any %bundle types yet. @link to add a new type.', [
- '%bundle' => 'Status',
- '@link' => $this->l($this->t('Go to the type creation page'), Url::fromRoute('entity.status_type.add_form')),
- ]),
- );
- }
- return array('#theme' => 'status_content_add_list', '#content' => $types);
- }
-
- public function addForm(EntityInterface $status_type, Request $request) {
- $entity = $this->storage->create(array(
- 'type' => $status_type->id()
- ));
- return $this->entityFormBuilder()->getForm($entity);
- }
-
- public function getAddFormTitle(EntityInterface $status_type) {
- return t('Create of bundle @label',
- array('@label' => $status_type->label())
- );
- }
- }
|