Source of file LinkField.php
Size: 4,277 Bytes - Last Modified: 2021-12-24T05:18:49+00:00
/var/www/docs.ssmods.com/process/src/src/Forms/LinkField.php
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185 | <?php /** * A simple set of fields to allow users to either enter an external URL or select an internal page * Expects value to be an array or serialized array: * array( * [Location] => Internal | External, * [NewTab] => bool, * [Internal] => int - SiteTree ID * [External] => string * ); */ namespace IQnection\Forms; use SilverStripe\Forms\TextField; use SilverStripe\Forms\CheckboxField; use SilverStripe\Forms\OptionsetField; use SilverStripe\View\Requirements; class LinkField extends TextField { protected $fieldHolderTemplate = 'Forms\LinkField'; private static $url_handlers = array( '$Action!/$ID' => '$Action' ); private static $allowed_actions = array( 'tree' ); public function __construct($name, $title = null, $value = null, $form = null) { parent::__construct($name, $title, $value, null, $form); } protected $_internalField; protected function InternalField() { if (!$this->_internalField) { $this->_internalField = LinkFieldTreeDropdownField::create($this->getName().'[Internal]','Internal Page',$this->getName()); } return $this->_internalField; } public function getInternalField() { return $this->InternalField(); } public function setInternalField($field) { $this->_internalField = $field; } protected $_externalField; protected function ExternalField() { if (!$this->_externalField) { $this->_externalField = TextField::create($this->getName().'[External]','External URL'); } return $this->_externalField; } public function getExternalField() { return $this->ExternalField(); } public function setExternalField($field) { $this->_externalField = $field; } protected $_locationField; protected function LocationField() { if (!$this->_locationField) { $this->_locationField = OptionsetField::create($this->getName().'[Location]','Location',array('Internal' => 'Internal','External' => 'External')); } return $this->_locationField; } public function getLocationField() { return $this->LocationField(); } public function setLocationField($field) { $this->_locationField = $field; } protected $_newTabField; protected function NewTabField() { if (!$this->_newTabField) { $this->_newTabField = CheckboxField::create($this->getName().'[NewTab]','Open in new tab'); } return $this->_newTabField; } public function getNewTabField() { return $this->NewTabField(); } public function setNewTabField($field) { $this->_newTabField = $field; } public function Field($properties = array()) { Requirements::javascript("iqnection-modules/linkfield:javascript/LinkField.js"); $SelectField = $this->getLocationField(); $ExternalField = $this->getExternalField()->addExtraClass('linkfieldexternal') ->setDescription('http://www.example.com') ->setAttribute('placeholder','http://www.example.com');; $InternalField = $this->getInternalField()->setForm($this->getForm())->addExtraClass('linkfieldinternal'); $NewTabField = $this->getNewTabField(); $defaults = unserialize($this->value); // set defaults switch($defaults['Location']) { case 'Internal': { $InternalField->setValue($defaults['Internal']); $SelectField->setValue('Internal'); break; } default: case 'External': { $ExternalField->setValue($defaults['External']); $SelectField->setValue('External'); break; } } $NewTabField->setValue($defaults['NewTab']); return $SelectField->FieldHolder().$InternalField->FieldHolder().$ExternalField->FieldHolder().$NewTabField->FieldHolder(); } public function tree( \SilverStripe\Control\HTTPRequest $request) { return $this->getInternalField()->tree($request); } public function setValue($values,$data=null) { // set defaults $defaults = array( 'NewTab' => 0, 'Internal' => '', 'External' => '', 'Location' => 'External' ); if (!is_array($values)) { $values = unserialize($values); } if (!is_array($values)) { $values = array($values); } $this->value = array_merge($defaults,$values); if ($this->value['Location'] == 'External') { $this->value['Internal'] == ''; } else { $this->value['External'] == ''; } $this->value = serialize($this->value); return $this; } } |