Used in template instead of {@link Elements()} to wrap each element in
its' controller, making it easier to access and process form logic and
actions stored in {@link ElementController}.

@return ArrayList
@throws \Exception
340394
public function ElementControllers()
    {
        // Don't try and process unsaved lists
        if ($this->Elements() instanceof UnsavedRelationList) {
            return ArrayList::create();
        }

        $controllers = ArrayList::create();
        $items = $this->Elements()->filterByCallback(function (BaseElement $item) {
            return $item->canView();
        });

        if (!is_null($items)) {
            foreach ($items as $element) {
                $controller = $element->getController();
                $controllers->push($controller);
            }
        }

        return $controllers;
    }
751653
public function renderElements()
	{
		$output='';
		foreach($this->getElements() as $element)
			$output.=$this->renderElement($element);
		return $output;
	}
423794
public function addElementArray(array $elements) {

		if(count($elements)) {
			
			$firstElement = array_shift($elements);
			$name = $firstElement->getName();

			// remove any array indexes from name

			$name = preg_replace('~\[\w*\]$~i', '', $name);

			if(!empty($this->elements[$name])) {

				throw new HtmlFormException(sprintf("Element '%s' already assigned.", $name));

			}

			$arrayName = $name . '[]';
			$firstElement
				->setName($arrayName)
				->setForm($this)
			;

			$this->elements[$name] = [$firstElement];

			foreach($elements as $e) {
				
				if(get_class($e) !== get_class($firstElement)) {

					throw new HtmlFormException(sprintf("Class mismatch of form elements array. Expected '%s', found '%s'.", get_class($firstElement), get_class($e)));

				}
				
				// check whether names of element arrays match

				$nameToCheck = preg_replace('~\[\w*\]$~i', '', $e->getName());

				if($nameToCheck && $nameToCheck !== $name) {
					
					throw new HtmlFormException(sprintf("Name mismatch of form elements array. Expected '%s' or empty name, found '%s'.", $name, $e->getName()));

				}

				$e
					->setName($arrayName)
					->setForm($this)
				;

				$this->elements[$name][] = $e;
			}

		}

		return $this;

	}
571570
public function onGetTemplateList()
    {
        $this->validateRequestTheme();

        $page = Page::inTheme($this->theme);
        return [
            'layouts' => $page->getLayoutOptions()
        ];
    }
639200
public function ComponentListingItems()
    {
        $manyMany = singleton($this->ListType)->config()->many_many;
        $tagClass = isset($manyMany[$this->ComponentFilterName]) ? $manyMany[$this->ComponentFilterName] : '';
        if (!$tagClass) {
            return new ArrayList();
        }
        $result = DataList::create($tagClass);
        if ($this->ComponentFilterWhere
            && ($componentWhereFilters = $this->ComponentFilterWhere->getValue())
        ) {
            $result = $result->filter($componentWhereFilters);
        }
        return $result;
    }
216122
public function controllerList()
    {
        $names = $this->controllerManager->getCanonicalNames();

        $controllers = array();

        foreach ($names as $name) {
            try {
                $controllers[$name] = $this->controllerManager->get($name);
            } catch (\Exception $e) {
                continue;
            }
        }

        return $controllers;
    }
1331486
public function prepare()
    {
        if ($this->ready) {
            return null;
        }

        $this->ready = true;

        $controller = $this->fullController;

        $methods = get_class_methods($controller);
        $allowedMethods = array_keys(self::$valids);

        $classMethods = array_intersect($methods, $allowedMethods);

        if (empty($classMethods)) {
            throw new Exception($controller . ' controller exists, but is not a valid', 2);
        }

        $contentType = $this->contentType . '; charset=' . $this->charset;

        foreach ($classMethods as $method) {
            $route = empty(self::$valids[$method]) ? false : self::$valids[$method];

            if ($route) {
                Route::set($route[0], $route[1], function ()
                use ($method, $contentType, $controller) {
                    header('Content-Type: ' . $contentType);

                    return call_user_func_array(array(new $controller, $method), func_get_args());
                });
            }
        }
    }
495687
protected function locateElements()
	{
		$elements = $this->locator->findAll();

		if ( empty($elements) ) {
			throw new ElementNotFoundException('No elements found by selector: ' . (string)$this->locator);
		}

		return $elements;
	}
953826
public function getController()
    {
        if ($this->controller) {
            return $this->controller;
        }
        foreach (array_reverse(ClassInfo::ancestry($this->class)) as $blockClass) {
            $controllerClass = "{$blockClass}Controller";
            if (class_exists($controllerClass)) {
                break;
            }
        }
        if (!class_exists($controllerClass)) {
            throw new ValidationException("Could not find controller class for $this->classname");
        }

        $this->controller = Injector::inst()->create($controllerClass, $this);
        $this->controller->init();

        return $this->controller;
    }
1211203
private function getElementsToProcess(): array
    {
        $result = [];

        foreach (get_object_vars($this) as $element) {
            if ($element instanceof FormElementInterface) {
                $result[] = $element;
            }
        }

        $result = array_merge($result, $this->extraElements);

        return $result;
    }
635266
public function getElement()
	{
		$element = $this->_element;

		if (empty($element))
		{
			$r = new ReflectionClass($this);
			if ($r->inNamespace())
			{
				$bits = explode('\\', __NAMESPACE__);

				// Should match either:
				//   Plugins\Folder\Element
				//   Components\Folder\Plugins\Element
				$element = strtolower($bits[2]);

				if (strtolower($bits[0]) == 'components')
				{
					$element = strtolower($bits[3]);
				}
			}
			else
			{
				throw new Exception('Cannot get or parse view class name.', 500);
			}
		}

		return $element;
	}
1109178
public function ElementsByName( $names )
	{
		if ( is_string( $names ) )
			$names = array_map( 'trim', explode( ",", $names ) );

		if ( ! is_array( $names ) )
			throw new Exception( 'InstanceElementsFilter::ElementsByName $names must be a string or array' );

		$names = array_flip( $names );
		return new InstanceElementsFilter( $this->instance, array_intersect_key( $this->elements, $names ) );
	}
1011015
public function getController()
    {
        if ($this->controller) {
            return $this->controller;
        }

        foreach (array_reverse(ClassInfo::ancestry(get_class($this))) as $widgetClass) {
            $controllerClass = "{$widgetClass}Controller";
            if (class_exists($controllerClass)) {
                break;
            }
        }

        if (!class_exists($controllerClass)) {
            throw new Exception('Could not find controller class for ' . static::class);
        }

        $this->controller = Injector::inst()->create($controllerClass, $this);
        if (Injector::inst()->has(HTTPRequest::class)) {
            $this->controller->setRequest(Injector::inst()->get(HTTPRequest::class));
        }

        return $this->controller;
    }
134011
protected function getChildren()
    {
        $retArray = [];

        foreach ($this->elements as $element => $occurrence) {
            switch ($occurrence) {
                case 'REQUIRED':
                case self::REQUIRED:
                    if (property_exists($this, $element)) {
                        $retArray[] = [
                            'name'          => $this->ns.$element,
                            'attributes'    => array_filter($this->$element->attributes),
                            'value'         => $this->$element
                        ];
                    } else {
                        die(
                            $this->getShortName(get_called_class()) .": Required element '{$element}' missing."
                        );
                    }
                    break;
                case 'OPTIONAL':
                case self::OPTIONAL:
                    if (property_exists($this, $element)) {
                        $retArray[] = [
                            'name'          => $this->ns.$element,
                            'attributes'    => array_filter($this->$element->attributes),
                            'value'         => $this->$element
                        ];
                    }
                    break;
                case 'REQUIRED_MULTI':
                case self::REQUIRED_MULTI:
                    if (property_exists($this, $element)) {
                        foreach ($this->$element as $child) {
                            $retArray[] = [
                                'name'          => $this->ns.$element,
                                'attributes'    => array_filter($child->attributes),
                                'value'         => $child
                            ];
                        }
                    } else {
                        die(
                            $this->getShortName(get_called_class()) .": Required element '{$element}' missing."
                        );
                    }
                    break;
                case 'OPTIONAL_MULTI':
                case self::OPTIONAL_MULTI:
                    if (property_exists($this, $element)) {
                        foreach ($this->$element as $child) {
                            $retArray[] = [
                                'name'          => $this->ns.$element,
                                'attributes'    => array_filter($child->attributes),
                                'value'         => $child
                            ];
                        }
                    }
                    break;
                default:
                    die(
                        $this->getShortName(get_called_class()) .": Unknown occurence type for element {$element}."
                    );
            }
        }

        return $retArray;
    }
865406
public function loadDefaultElements()
    {
        if ($this->getAskOld()) {
            $this->getOldPasswordElement();
        }
        if ($this->getAskCheck()) {
            $this->addCheckFields();
        }

        $this->getNewPasswordElement();
        $this->getRepeatPasswordElement();
        $this->getSubmitButton();

        if (! $this->forceRules) {
            $this->getReportNoEnforcementElement();
        }

        if ($this->reportRules) {
            $this->getReportRulesElement();
        }

        if ($this->useTableLayout) {
            /****************
             * Display form *
             ****************/
            $this->_table = new \MUtil_Html_TableElement(array('class' => 'formTable'));
            $this->_table->setAsFormLayout($this, true, true);
            $this->_table['tbody'][0][0]->class = 'label';  // Is only one row with formLayout, so all in output fields get class.
        }

        return $this;
    }
711622
public function getElements(Element $wrapper = null)
    {
        $elements = [];

        $label = $this->getLabelElement();

        if ($label instanceof Element) {
            $elements[] = $label;
        }

        $element = $this->getElement();

        if (!$element->getAttribute('id')) {
            $element->addAttribute('id', $this->getIdAttribute());
        }

        if (!$this->isValid()) {
            $element->addClass('is-invalid');
        }

        $elements[] = $element;

        if (!$this->isValid()) {
            $error = $this->getForm()->createInvalidElement();
            $error->setInnerText($this->getErrorMessage());
            $elements[] = $error;
        }

        return $elements;
    }
617869
public function parseElements(array $elements, ElementInterface $parent = null)
    {
        foreach ($elements as $element) {
            try {
                $object = $this->elementFactory->make($element['name']);
            } catch (InvalidElementException $e) {
                $object = $this->getReservation($element['name']);
            }

            // Set some stuff
            $object->setCollection($this->collection);
            $object->setAttributes($element['attributes']);

            if ( ! is_array($element['value'])) {
                $object->setValue(trim($element['value']));
            }

            // Add the object to the collection
            $this->collection->add($object);

            if ($parent) {
                $object->setParent($parent);
                $parent->addChild($object);
            }

            if (is_array($element['value'])) {
                $this->parseElements($element['value'], $object);
            }

            // Initialize object
            $object->init();

            if ($object instanceof ReservationInterface) {
                $object = $object->getElement($this, $parent);
            }

            $data[] = $object;
        }

        return $data;
    }