silverstripe-cms/code/Forms/AnchorSelectorField.php

62 lines
1.3 KiB
PHP
Raw Permalink Normal View History

2017-09-05 04:38:38 +02:00
<?php
namespace SilverStripe\CMS\Forms;
use SilverStripe\CMS\Model\SiteTree;
2017-09-05 04:38:38 +02:00
use SilverStripe\Control\HTTPRequest;
use SilverStripe\Forms\TextField;
/**
* Assists with selecting anchors on a given page
*/
class AnchorSelectorField extends TextField
{
protected $schemaComponent = 'AnchorSelectorField';
private static $allowed_actions = [
'anchors',
];
private static $url_handlers = [
'anchors/$PageID' => 'anchors',
];
public function getSchemaDataDefaults()
{
$schema = parent::getSchemaDataDefaults();
$schema['data']['endpoint'] = $this->Link('anchors/:id');
return $schema;
}
/**
* Find all anchors available on the given page.
*
* @param HTTPRequest $request
* @return array
*/
public function anchors(HTTPRequest $request)
{
$id = (int)$this->getRequest()->param('PageID');
$anchors = $this->getAnchorsInPage($id);
2017-09-05 04:38:38 +02:00
return json_encode($anchors);
}
/**
* Get anchors in the given page ID.
2017-09-05 04:38:38 +02:00
*
* @param int $id
* @return array
*/
protected function getAnchorsInPage($id)
{
$page = SiteTree::get()->byID($id);
2017-09-05 04:38:38 +02:00
if (!$page || !$page->canView()) {
2017-09-05 04:38:38 +02:00
return [];
}
return $page->getAnchorsOnPage();
2017-09-05 04:38:38 +02:00
}
}