11 * Examines a form and determines the entities, fields & joins in use.
17 * Ex: $entities['spouse']['type'] = 'Contact';
24 protected $blocks = [];
26 public function __construct($layout) {
27 $root = AHQ
::makeRoot($layout);
28 $this->entities
= array_column(AHQ
::getTags($root, 'af-entity'), NULL, 'name');
29 foreach (array_keys($this->entities
) as $entity) {
30 $this->entities
[$entity]['fields'] = $this->entities
[$entity]['joins'] = [];
32 // Pre-load full list of afforms in case this layout embeds other afform directives
33 $this->blocks
= (array) Afform
::get()->setCheckPermissions(FALSE)->setSelect(['name', 'directive_name'])->execute()->indexBy('directive_name');
34 $this->parseFields($layout);
39 * @param string $entity
42 protected function parseFields($nodes, $entity = NULL, $join = NULL) {
43 foreach ($nodes as $node) {
44 if (!is_array($node) ||
!isset($node['#tag'])) {
47 elseif (!empty($node['af-fieldset']) && !empty($node['#children'])) {
48 $this->parseFields($node['#children'], $node['af-fieldset'], $join);
50 elseif ($entity && $node['#tag'] === 'af-field') {
52 $this->entities
[$entity]['joins'][$join]['fields'][$node['name']] = AHQ
::getProps($node);
55 $this->entities
[$entity]['fields'][$node['name']] = AHQ
::getProps($node);
58 elseif ($entity && !empty($node['af-join'])) {
59 $this->entities
[$entity]['joins'][$node['af-join']] = AHQ
::getProps($node);
60 $this->parseFields($node['#children'] ??
[], $entity, $node['af-join']);
62 elseif (!empty($node['#children'])) {
63 $this->parseFields($node['#children'], $entity, $join);
65 // Recurse into embedded blocks
66 if (isset($this->blocks
[$node['#tag']])) {
67 if (!isset($this->blocks
[$node['#tag']]['layout'])) {
68 $this->blocks
[$node['#tag']] = Afform
::get()->setCheckPermissions(FALSE)->setSelect(['name', 'layout'])->addWhere('name', '=', $this->blocks
[$node['#tag']]['name'])->execute()->first();
70 if (!empty($this->blocks
[$node['#tag']]['layout'])) {
71 $this->parseFields($this->blocks
[$node['#tag']]['layout'], $entity, $join);
79 * Ex: $entities['spouse']['type'] = 'Contact';
81 public function getEntities() {
82 return $this->entities
;