Source of file BlockLinkField.php
Size: 4,069 Bytes - Last Modified: 2021-12-23T10:29:56+00:00
/var/www/docs.ssmods.com/process/src/src/Form/BlockLinkField.php
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169 | <?php namespace SilverStripe\ElementalBannerBlock\Form; use SilverStripe\CMS\Model\SiteTree; use SilverStripe\Core\Convert; use SilverStripe\Forms\FormField; use SilverStripe\ORM\DataObject; use SilverStripe\View\ArrayData; class BlockLinkField extends FormField { protected $schemaDataType = FormField::SCHEMA_DATA_TYPE_CUSTOM; protected $schemaComponent = 'BlockLinkField'; /** * Cache for parsed value * * @var ArrayData */ protected $parsedValue; /** * Whether to show the "link text" field * * @var bool */ protected $showLinkText = true; public function Type() { return 'block-link-field'; } /** * Reset the cached parsed value when setting a new value * * {@inheritDoc} */ public function setValue($value, $data = null) { $this->parsedValue = null; return parent::setValue($value, $data); } /** * Return a parsed {@link ArrayData} object from the contents of the JSON value. Will * be cached for future use, and the cache will be reset via setValue each time. * * @return ArrayData */ public function getParsedValue() { if ($this->parsedValue) { return $this->parsedValue; } $parsedValue = json_decode($this->dataValue(), true); return $this->parsedValue = ArrayData::create((array) $parsedValue); } /** * Get the linked {@link SiteTree} object, if available * * @return SiteTree|null */ public function getLinkPage() { $pageId = (int) $this->getParsedValue()->PageID; if (!$pageId) { return null; } return DataObject::get_by_id(SiteTree::class, $pageId); } /** * Get the link text/title * * @return string */ public function getLinkText() { return trim($this->getParsedValue()->Text); } /** * Get the link "description", used for titles or alt text * * @return string */ public function getLinkDescription() { return $this->getParsedValue()->Description; } /** * Get whether to open the link in a new window * * @return bool */ public function getLinkTargetBlank() { return (bool) $this->getParsedValue()->TargetBlank; } /** * Get the relative URL for the linked {@link SiteTree} object, with a leading slash * * @return string */ public function getLinkRelativeUrl() { $page = $this->getLinkPage(); return $page ? '/' . ltrim($page->URLSegment, '/') : ''; } /** * Set whether to display the link text field * * @param bool $showLinkText * @return $this */ public function setShowLinkText($showLinkText) { $this->showLinkText = (bool) $showLinkText; return $this; } /** * Get whether to display the link text field * * @return bool */ public function getShowLinkText() { return $this->showLinkText; } /** * When not used in a React form factory context, this adds the schema and state data to SilverStripe * template rendered attributes lists * * @return array */ public function getAttributes() { $attributes = parent::getAttributes(); $attributes['data-schema'] = json_encode($this->getSchemaData()); $attributes['data-state'] = json_encode($this->getSchemaState()); return $attributes; } /** * Add some extra props for the React component to work with * * {@inheritDoc} */ public function getSchemaDataDefaults() { $schema = parent::getSchemaDataDefaults(); $schema['data'] = array_merge($schema['data'], [ 'showLinkText' => $this->getShowLinkText(), 'linkedPage' => $this->getLinkPage() ? $this->getLinkPage()->toMap() : [], 'title' => $this->Title(), ]); return $schema; } } |