Source of file EditableLiteralField.php
Size: 4,625 Bytes - Last Modified: 2021-12-23T10:19:14+00:00
/var/www/docs.ssmods.com/process/src/code/Model/EditableFormField/EditableLiteralField.php
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171 | <?php namespace SilverStripe\UserForms\Model\EditableFormField; use SilverStripe\Core\Injector\Injector; use SilverStripe\Forms\HTMLEditor\HTMLEditorConfig; use SilverStripe\Forms\HTMLEditor\HTMLEditorField; use SilverStripe\Forms\HTMLEditor\HTMLEditorSanitiser; use SilverStripe\Forms\CheckboxField; use SilverStripe\Forms\CompositeField; use SilverStripe\Forms\LiteralField; use SilverStripe\UserForms\Model\EditableFormField; /** * Editable Literal Field. A literal field is just a blank slate where * you can add your own HTML / Images / Flash * * @package userforms */ class EditableLiteralField extends EditableFormField { private static $singular_name = 'HTML Block'; private static $plural_name = 'HTML Blocks'; private static $table_name = 'EditableLiteralField'; /** * Mark as literal only * * @config * @var bool */ private static $literal = true; /** * Get the name of the editor config to use for HTML sanitisation. Defaults to the active config. * * @var string * @config */ private static $editor_config = null; private static $db = [ 'Content' => 'HTMLText', // From CustomSettings 'HideFromReports' => 'Boolean(0)', // from CustomSettings 'HideLabel' => 'Boolean(0)' ]; private static $defaults = [ 'HideFromReports' => false ]; /** * Returns the {@see HTMLEditorConfig} instance to use for sanitisation * * @return HTMLEditorConfig */ protected function getEditorConfig() { $editorConfig = $this->config()->get('editor_config'); if ($editorConfig) { return HTMLEditorConfig::get($editorConfig); } return HTMLEditorConfig::get_active(); } /** * Safely sanitise html content, if enabled * * @param string $content Raw html * @return string Safely sanitised html */ protected function sanitiseContent($content) { // Check if sanitisation is enabled if (!HTMLEditorField::config()->get('sanitise_server_side')) { return $content; } // Perform sanitisation $htmlValue = Injector::inst()->create('HTMLValue', $content); $santiser = Injector::inst()->create(HTMLEditorSanitiser::class, $this->getEditorConfig()); $santiser->sanitise($htmlValue); return $htmlValue->getContent(); } /** * Get HTML Content of this literal field * * @return string */ public function getContent() { // Apply html editor sanitisation rules $content = $this->getField('Content'); return $this->sanitiseContent($content); } /** * Set the content with the given value * * @param string $content */ public function setContent($content) { // Apply html editor sanitisation rules $content = $this->sanitiseContent($content); $this->setField('Content', $content); } /** * @return FieldList */ public function getCMSFields() { $fields = parent::getCMSFields(); $fields->removeByName(['Default', 'Validation', 'RightTitle']); $fields->addFieldsToTab('Root.Main', [ HTMLEditorField::create('Content', _t(__CLASS__.'.CONTENT', 'HTML')) ->setRows(4) ->setColumns(20), CheckboxField::create( 'HideFromReports', _t(__CLASS__.'.HIDEFROMREPORT', 'Hide from reports?') ), CheckboxField::create( 'HideLabel', _t(__CLASS__.'.HIDELABEL', "Hide 'Title' label on frontend?") ) ]); return $fields; } public function getFormField() { $content = LiteralField::create( "LiteralFieldContent-{$this->ID}]", $this->dbObject('Content')->forTemplate() ); $field = CompositeField::create($content) ->setName($this->Name) // ->setID($this->Name) // @todo: https://github.com/silverstripe/silverstripe-framework/issues/7264 ->setFieldHolderTemplate(__CLASS__ . '_holder'); $this->doUpdateFormField($field); return $field; } protected function updateFormField($field) { parent::updateFormField($field); if ($this->HideLabel) { $this->ExtraClass .= ' nolabel'; } else { $field->setTitle($this->Title); } } public function showInReports() { return !$this->HideFromReports; } } |