StatusAddController.php 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. <?php
  2. namespace Drupal\statusmessage\Controller;
  3. use Drupal\Core\Controller\ControllerBase;
  4. use Drupal\Core\Entity\EntityInterface;
  5. use Drupal\Core\Entity\EntityTypeManagerInterface;
  6. use Drupal\Core\Entity\EntityStorageInterface;
  7. use Drupal\Core\Url;
  8. use Symfony\Component\DependencyInjection\ContainerInterface;
  9. use Symfony\Component\HttpFoundation\Request;
  10. /**
  11. * Class StatusAddController.
  12. *
  13. * @package Drupal\statusmessage\Controller
  14. */
  15. class StatusAddController extends ControllerBase {
  16. public function __construct(EntityStorageInterface $storage, EntityStorageInterface $type_storage) {
  17. $this->storage = $storage;
  18. $this->typeStorage = $type_storage;
  19. }
  20. /**
  21. * {@inheritdoc}
  22. */
  23. public static function create(ContainerInterface $container) {
  24. /** @var EntityTypeManagerInterface $entity_type_manager */
  25. $entity_type_manager = $container->get('entity_type.manager');
  26. return new static(
  27. $entity_type_manager->getStorage('status'),
  28. $entity_type_manager->getStorage('status_type')
  29. );
  30. }
  31. /**
  32. * Displays add links for available bundles/types for entity status .
  33. *
  34. * @param \Symfony\Component\HttpFoundation\Request $request
  35. * The current request object.
  36. *
  37. * @return array
  38. * A render array for a list of the status bundles/types that can be added or
  39. * if there is only one type/bunlde defined for the site, the function returns the add page for that bundle/type.
  40. */
  41. public function add(Request $request) {
  42. $types = $this->typeStorage->loadMultiple();
  43. if ($types && count($types) == 1) {
  44. $type = reset($types);
  45. return $this->addForm($type, $request);
  46. }
  47. if (count($types) === 0) {
  48. return array(
  49. '#markup' => $this->t('You have not created any %bundle types yet. @link to add a new type.', [
  50. '%bundle' => 'Status',
  51. '@link' => $this->l($this->t('Go to the type creation page'), Url::fromRoute('entity.status_type.add_form')),
  52. ]),
  53. );
  54. }
  55. return array('#theme' => 'status_content_add_list', '#content' => $types);
  56. }
  57. /**
  58. * Presents the creation form for status entities of given bundle/type.
  59. *
  60. * @param EntityInterface $status_type
  61. * The custom bundle to add.
  62. * @param \Symfony\Component\HttpFoundation\Request $request
  63. * The current request object.
  64. *
  65. * @return array
  66. * A form array as expected by drupal_render().
  67. */
  68. public function addForm(EntityInterface $status_type, Request $request) {
  69. $entity = $this->storage->create(array(
  70. 'type' => $status_type->id()
  71. ));
  72. return $this->entityFormBuilder()->getForm($entity);
  73. }
  74. /**
  75. * Provides the page title for this controller.
  76. *
  77. * @param EntityInterface $status_type
  78. * The custom bundle/type being added.
  79. *
  80. * @return string
  81. * The page title.
  82. */
  83. public function getAddFormTitle(EntityInterface $status_type) {
  84. return t('Create of bundle @label',
  85. array('@label' => $status_type->label())
  86. );
  87. }
  88. }