mirror of
https://github.com/silverstripe/silverstripe-cms
synced 2024-10-22 08:05:56 +02:00
NEW add SiteTree::updateAnchorsOnPage() for user defining additional page anchors
This commit is contained in:
parent
07e70148da
commit
f2ebdb7f5e
@ -2,7 +2,7 @@
|
||||
|
||||
namespace SilverStripe\CMS\Forms;
|
||||
|
||||
use Page;
|
||||
use SilverStripe\CMS\Model\SiteTree;
|
||||
use SilverStripe\Control\HTTPRequest;
|
||||
use SilverStripe\Forms\TextField;
|
||||
|
||||
@ -38,34 +38,24 @@ class AnchorSelectorField extends TextField
|
||||
{
|
||||
$id = (int)$this->getRequest()->param('PageID');
|
||||
$anchors = $this->getAnchorsInPage($id);
|
||||
|
||||
return json_encode($anchors);
|
||||
}
|
||||
|
||||
/**
|
||||
* Get anchors in the given page ID
|
||||
* Get anchors in the given page ID.
|
||||
*
|
||||
* @param int $id
|
||||
* @return array
|
||||
*/
|
||||
protected function getAnchorsInPage($id)
|
||||
{
|
||||
// Check page is accessible
|
||||
$page = Page::get()->byID($id);
|
||||
$page = SiteTree::get()->byID($id);
|
||||
|
||||
if (!$page || !$page->canView()) {
|
||||
return [];
|
||||
}
|
||||
|
||||
// Similar to the regex found in HtmlEditorField.js / getAnchors method.
|
||||
$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]))));
|
||||
return $page->getAnchorsOnPage();
|
||||
}
|
||||
}
|
||||
|
@ -3070,4 +3070,28 @@ class SiteTree extends DataObject implements PermissionProvider, i18nEntityProvi
|
||||
$this->extend('updateExcludedURLSegments', $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