stephen-makrogianni
5/11/2020 - 2:59 AM

WIP - Elemental anchors

Issue:

The WYSIWYG field in content blocks does not locate all anchors on the page.

Proposed solution

Create a hook on SiteTree::getAnchorsOnPage() that gets the selected page's anchors categorised by the Elemental block they are in.

This will prove the user with a dropdown filed similar to the tree drop down field where they could select the Elemental block to anchor to or one of it's "children" (anchors in the WYSIWYG).

Things to note:

The existing hook updateAnchorsOnPage in SiteTree::getAnchorsOnPage() is below the check $parseSuccess meaning that there is no way to reach it with the proposed solution.

How to solve the issue

Move updateAnchorsOnPage in SiteTree::getAnchorsOnPage() above the check to ensure Add an extension to Elemental to hook into SiteTree::getAnchorsOnPage()

AnchorSelectorField::class

/**
 * @return array
 */
public function getAnchorsOnPage()
{
    $parseSuccess = preg_match_all(
        "/\\s+(name|id)\\s*=\\s*([\"'])([^\\2\\s>]*?)\\2|\\s+(name|id)\\s*=\\s*([^\"']+)[\\s +>]/im",
        $this->Content,
        $matches
    );

    $foobar;
    $stuff = [];

    if ($this->supportsElemental()) {
        $elementalAreas = $this->getElementalRelations();

        foreach ($elementalAreas as $areaName) {
            $name = $this->$areaName;
            foreach ($this->$areaName->Elements() as $element) {

//                    $gridField = $element->Fields()->dataFields();
//                    $gridField = $element->Fields()->dataFieldByName($this->sanitiseClassName($this->modelClass));
                $foo = $element->HTML;
                $bar = $element->getBlockSchema();
                $hh = $element->getCMSFields();
                $foobar = preg_match_all(
                    "/\\s+(name|id)\\s*=\\s*([\"'])([^\\2\\s>]*?)\\2|\\s+(name|id)\\s*=\\s*([^\"']+)[\\s +>]/im",
                    $element->HTML,
                    $matches
                );

                if ($foobar) {
                    $stuff = array_merge($stuff, array_merge($matches[3], $matches[5]));
                }
            }
        }
    }

    if (!count($stuff) > 0) {
        return [];
    }

    $anchors = array_values(array_unique(array_filter($stuff)));

    $this->extend('updateAnchorsOnPage', $anchors);

    return $anchors;
}