mirror of
https://github.com/silverstripe/silverstripe-cms
synced 2024-10-22 08:05:56 +02:00
Merge pull request #2214 from wilr/patches/anchor-extension
NEW add SiteTree::updateAnchorsOnPage() for defining content anchors
This commit is contained in:
commit
d1112f25b9
@ -2,7 +2,7 @@
|
|||||||
|
|
||||||
namespace SilverStripe\CMS\Forms;
|
namespace SilverStripe\CMS\Forms;
|
||||||
|
|
||||||
use Page;
|
use SilverStripe\CMS\Model\SiteTree;
|
||||||
use SilverStripe\Control\HTTPRequest;
|
use SilverStripe\Control\HTTPRequest;
|
||||||
use SilverStripe\Forms\TextField;
|
use SilverStripe\Forms\TextField;
|
||||||
|
|
||||||
@ -38,34 +38,24 @@ class AnchorSelectorField extends TextField
|
|||||||
{
|
{
|
||||||
$id = (int)$this->getRequest()->param('PageID');
|
$id = (int)$this->getRequest()->param('PageID');
|
||||||
$anchors = $this->getAnchorsInPage($id);
|
$anchors = $this->getAnchorsInPage($id);
|
||||||
|
|
||||||
return json_encode($anchors);
|
return json_encode($anchors);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Get anchors in the given page ID
|
* Get anchors in the given page ID.
|
||||||
*
|
*
|
||||||
* @param int $id
|
* @param int $id
|
||||||
* @return array
|
* @return array
|
||||||
*/
|
*/
|
||||||
protected function getAnchorsInPage($id)
|
protected function getAnchorsInPage($id)
|
||||||
{
|
{
|
||||||
// Check page is accessible
|
$page = SiteTree::get()->byID($id);
|
||||||
$page = Page::get()->byID($id);
|
|
||||||
if (!$page || !$page->canView()) {
|
if (!$page || !$page->canView()) {
|
||||||
return [];
|
return [];
|
||||||
}
|
}
|
||||||
|
|
||||||
// Similar to the regex found in HtmlEditorField.js / getAnchors method.
|
return $page->getAnchorsOnPage();
|
||||||
$parseSuccess = preg_match_all(
|
|
||||||
"/\\s+(name|id)\\s*=\\s*([\"'])([^\\2\\s>]*?)\\2|\\s+(name|id)\\s*=\\s*([^\"']+)[\\s +>]/im",
|
|
||||||
$page->Content,
|
|
||||||
$matches
|
|
||||||
);
|
|
||||||
if (!$parseSuccess) {
|
|
||||||
return [];
|
|
||||||
}
|
|
||||||
|
|
||||||
// Cleanup results
|
|
||||||
return array_values(array_unique(array_filter(array_merge($matches[3], $matches[5]))));
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -3093,4 +3093,28 @@ class SiteTree extends DataObject implements PermissionProvider, i18nEntityProvi
|
|||||||
$this->extend('updateExcludedURLSegments', $excludes);
|
$this->extend('updateExcludedURLSegments', $excludes);
|
||||||
return $excludes;
|
return $excludes;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @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
|
||||||
|
);
|
||||||
|
|
||||||
|
if (!$parseSuccess) {
|
||||||
|
return [];
|
||||||
|
}
|
||||||
|
|
||||||
|
$anchors = array_values(array_unique(array_filter(
|
||||||
|
array_merge($matches[3], $matches[5])
|
||||||
|
)));
|
||||||
|
|
||||||
|
$this->extend('updateAnchorsOnPage', $anchors);
|
||||||
|
|
||||||
|
return $anchors;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user