Skouter mortgage estimates. Web application with view written in PHP and Vue, but controller and models in Go.
25'ten fazla konu seçemezsiniz Konular bir harf veya rakamla başlamalı, kısa çizgiler ('-') içerebilir ve en fazla 35 karakter uzunluğunda olabilir.

FlexFormFactory.php 2.3 KiB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. <?php
  2. declare(strict_types=1);
  3. namespace Grav\Plugin\FlexObjects;
  4. use Grav\Common\Grav;
  5. use Grav\Common\Page\Interfaces\PageInterface;
  6. use Grav\Common\Page\Page;
  7. use Grav\Framework\Form\Interfaces\FormFactoryInterface;
  8. use Grav\Framework\Form\Interfaces\FormInterface;
  9. use RocketTheme\Toolbox\Event\Event;
  10. use function is_callable;
  11. use function is_string;
  12. /**
  13. * Class FlexFormFactory
  14. * @package Grav\Plugin\FlexObjects
  15. */
  16. class FlexFormFactory implements FormFactoryInterface
  17. {
  18. /**
  19. * @param Page $page
  20. * @param string $name
  21. * @param array $form
  22. * @return FormInterface|null
  23. */
  24. public function createPageForm(Page $page, string $name, array $form): ?FormInterface
  25. {
  26. return $this->createFormForPage($page, $name, $form);
  27. }
  28. /**
  29. * @param PageInterface $page
  30. * @param string $name
  31. * @param array $form
  32. * @return FormInterface|null
  33. */
  34. public function createFormForPage(PageInterface $page, string $name, array $form): ?FormInterface
  35. {
  36. // Fire event
  37. $grav = Grav::instance();
  38. $grav->fireEvent('onBeforeFlexFormInitialize', new Event(['page' => $page, 'name' => $name, 'form' => &$form]));
  39. $page->addForms([$form], true);
  40. $formFlex = $form['flex'] ?? [];
  41. $type = $formFlex['type'] ?? null;
  42. $key = $formFlex['key'] ?? null;
  43. if (null !== $key && !is_string($key)) {
  44. $key = (string)$key;
  45. }
  46. $layout = $formFlex['layout'] ?? $name;
  47. /** @var Flex $flex */
  48. $flex = Grav::instance()['flex_objects'];
  49. if (is_string($type)) {
  50. $directory = $flex->getDirectory($type);
  51. if (!$directory) {
  52. return null;
  53. }
  54. $create = $form['actions']['create'] ?? true;
  55. $edit = $form['actions']['edit'] ?? true;
  56. $object = $edit && null !== $key ? $directory->getObject($key) : null;
  57. if ($object) {
  58. if (is_callable([$object, 'refresh'])) {
  59. $object->refresh();
  60. }
  61. } elseif ($create) {
  62. $object = $directory->createObject([], $key ?? '');
  63. }
  64. } else {
  65. $object = $flex->getObject($key);
  66. }
  67. return $object ? $object->getForm($layout, ['form' => $form]) : null;
  68. }
  69. }