silverstripe-userforms/code/model/editableformfields/EditableFormField.php

907 lines
23 KiB
PHP
Raw Normal View History

<?php
2015-08-11 22:57:19 +02:00
use SilverStripe\Forms\SegmentField;
/**
2015-08-11 22:57:19 +02:00
* Represents the base class of a editable form field
* object like {@link EditableTextField}.
*
* @package userforms
2015-08-11 22:57:19 +02:00
*
* @property string $Name
* @property string $Title
* @property string $Default
* @property int $Sort
* @property bool $Required
* @property string $CustomErrorMessage
* @method UserDefinedForm Parent() Parent page
2015-07-24 04:37:48 +02:00
* @method DataList DisplayRules() List of EditableCustomRule objects
*/
class EditableFormField extends DataObject {
2015-07-24 04:37:48 +02:00
2015-08-11 08:21:43 +02:00
/**
* Set to true to hide from class selector
*
* @config
* @var bool
*/
private static $hidden = false;
/**
* Define this field as abstract (not inherited)
*
* @config
* @var bool
*/
private static $abstract = true;
/**
* Flag this field type as non-data (e.g. literal, header, html)
*
* @config
* @var bool
*/
private static $literal = false;
2015-07-24 04:37:48 +02:00
/**
* Default sort order
*
* @config
* @var string
*/
private static $default_sort = '"Sort"';
2015-08-11 22:57:19 +02:00
/**
* A list of CSS classes that can be added
*
* @var array
*/
public static $allowed_css = array();
2015-07-24 04:37:48 +02:00
/**
* @config
* @var array
*/
private static $summary_fields = array(
'Title'
);
/**
* @config
* @var array
*/
private static $db = array(
"Name" => "Varchar",
"Title" => "Varchar(255)",
2015-07-24 04:37:48 +02:00
"Default" => "Varchar(255)",
"Sort" => "Int",
"Required" => "Boolean",
"CustomErrorMessage" => "Varchar(255)",
2015-07-24 04:37:48 +02:00
"CustomRules" => "Text", // @deprecated from 2.0
"CustomSettings" => "Text", // @deprecated from 2.0
"Migrated" => "Boolean", // set to true when migrated
"ExtraClass" => "Text", // from CustomSettings
"RightTitle" => "Varchar(255)", // from CustomSettings
"ShowOnLoad" => "Boolean(1)", // from CustomSettings
);
private static $defaults = array(
2016-04-29 14:15:01 +02:00
'ShowOnLoad' => true,
);
2015-07-24 04:37:48 +02:00
/**
* @config
* @var array
*/
private static $has_one = array(
"Parent" => "UserDefinedForm",
);
2015-07-24 04:37:48 +02:00
/**
* Built in extensions required
*
* @config
* @var array
*/
private static $extensions = array(
"Versioned('Stage', 'Live')"
);
2015-07-24 04:37:48 +02:00
/**
* @config
* @var array
*/
private static $has_many = array(
"DisplayRules" => "EditableCustomRule.Parent" // from CustomRules
);
/**
* @var bool
*/
protected $readonly;
2015-07-24 04:37:48 +02:00
/**
* Set the visibility of an individual form field
*
* @param bool
2015-08-11 22:57:19 +02:00
*/
public function setReadonly($readonly = true) {
$this->readonly = $readonly;
}
/**
2015-08-11 22:57:19 +02:00
* Returns whether this field is readonly
*
* @return bool
*/
private function isReadonly() {
return $this->readonly;
}
2015-07-24 04:37:48 +02:00
/**
2015-07-24 04:37:48 +02:00
* @return FieldList
*/
public function getCMSFields() {
$fields = new FieldList(new TabSet('Root'));
// Main tab
$fields->addFieldsToTab(
'Root.Main',
array(
ReadonlyField::create(
'Type',
_t('EditableFormField.TYPE', 'Type'),
$this->i18n_singular_name()
),
LiteralField::create(
'MergeField',
_t(
'EditableFormField.MERGEFIELDNAME',
'<div class="field readonly">' .
'<label class="left">Merge field</label>' .
'<div class="middleColumn">' .
'<span class="readonly">$' . $this->Name . '</span>' .
'</div>' .
'</div>'
)
),
TextField::create('Title'),
TextField::create('Default', _t('EditableFormField.DEFAULT', 'Default value')),
2015-08-11 22:57:19 +02:00
TextField::create('RightTitle', _t('EditableFormField.RIGHTTITLE', 'Right title')),
SegmentField::create('Name')->setModifiers(array(
UnderscoreSegmentFieldModifier::create()->setDefault('FieldName'),
2015-08-11 22:57:19 +02:00
DisambiguationSegmentFieldModifier::create(),
))->setPreview($this->Name)
2015-07-24 04:37:48 +02:00
)
);
// Custom settings
if (!empty(self::$allowed_css)) {
$cssList = array();
foreach(self::$allowed_css as $k => $v) {
if (!is_array($v)) {
$cssList[$k]=$v;
} elseif ($k === $this->ClassName) {
$cssList = array_merge($cssList, $v);
}
}
$fields->addFieldToTab('Root.Main',
DropdownField::create(
'ExtraClass',
_t('EditableFormField.EXTRACLASS_TITLE', 'Extra Styling/Layout'),
$cssList
)->setDescription(_t(
'EditableFormField.EXTRACLASS_SELECT',
'Select from the list of allowed styles'
))
);
} else {
$fields->addFieldToTab('Root.Main',
TextField::create(
'ExtraClass',
_t('EditableFormField.EXTRACLASS_Title', 'Extra CSS classes')
2015-07-24 04:37:48 +02:00
)->setDescription(_t(
'EditableFormField.EXTRACLASS_MULTIPLE',
'Separate each CSS class with a single space'
))
);
}
// Validation
$validationFields = $this->getFieldValidationOptions();
if($validationFields && $validationFields->count()) {
$fields->addFieldsToTab('Root.Validation', $validationFields);
}
// Add display rule fields
$displayFields = $this->getDisplayRuleFields();
if($displayFields && $displayFields->count()) {
$fields->addFieldsToTab('Root.DisplayRules', $displayFields);
}
$this->extend('updateCMSFields', $fields);
return $fields;
}
/**
* Return fields to display on the 'Display Rules' tab
*
* @return FieldList
*/
protected function getDisplayRuleFields() {
// Check display rules
if($this->Required) {
return new FieldList(
LabelField::create(_t(
'EditableFormField.DISPLAY_RULES_DISABLED',
'Display rules are not enabled for required fields. ' .
'Please uncheck "Is this field Required?" under "Validation" to re-enable.'
))->addExtraClass('message warning')
);
}
2015-08-18 07:10:23 +02:00
$self = $this;
$allowedClasses = array_keys($this->getEditableFieldClasses(false));
2015-07-24 04:37:48 +02:00
$editableColumns = new GridFieldEditableColumns();
$editableColumns->setDisplayFields(array(
'Display' => '',
2015-08-18 07:10:23 +02:00
'ConditionFieldID' => function($record, $column, $grid) use ($allowedClasses, $self) {
2015-07-24 04:37:48 +02:00
return DropdownField::create(
$column,
'',
EditableFormField::get()
->filter(array(
2015-08-18 07:10:23 +02:00
'ParentID' => $self->ParentID,
'ClassName' => $allowedClasses
2015-07-24 04:37:48 +02:00
))
->exclude(array(
2015-08-18 07:10:23 +02:00
'ID' => $self->ID
2015-07-24 04:37:48 +02:00
))
->map('ID', 'Title')
);
},
'ConditionOption' => function($record, $column, $grid) {
$options = Config::inst()->get('EditableCustomRule', 'condition_options');
return DropdownField::create($column, '', $options);
},
'FieldValue' => function($record, $column, $grid) {
return TextField::create($column);
},
2015-08-18 07:10:23 +02:00
'ParentID' => function($record, $column, $grid) use ($self) {
return HiddenField::create($column, '', $self->ID);
2015-07-24 04:37:48 +02:00
}
));
// Custom rules
$customRulesConfig = GridFieldConfig::create()
->addComponents(
$editableColumns,
new GridFieldButtonRow(),
new GridFieldToolbarHeader(),
new GridFieldAddNewInlineButton(),
2015-08-14 04:51:42 +02:00
new GridFieldDeleteAction()
2015-07-24 04:37:48 +02:00
);
return new FieldList(
2015-07-24 04:37:48 +02:00
CheckboxField::create('ShowOnLoad')
->setDescription(_t(
'EditableFormField.SHOWONLOAD',
'Initial visibility before processing these rules'
)),
GridField::create(
'DisplayRules',
_t('EditableFormField.CUSTOMRULES', 'Custom Rules'),
$this->DisplayRules(),
$customRulesConfig
)
);
2015-07-24 04:37:48 +02:00
}
public function onBeforeWrite() {
parent::onBeforeWrite();
// Set a field name.
if(!$this->Name) {
// New random name
$this->Name = $this->generateName();
} elseif($this->Name === 'Field') {
throw new ValidationException('Field name cannot be "Field"');
}
2015-07-24 04:37:48 +02:00
if(!$this->Sort && $this->ParentID) {
$parentID = $this->ParentID;
$this->Sort = EditableFormField::get()
->filter('ParentID', $parentID)
->max('Sort') + 1;
}
}
/**
* Generate a new non-conflicting Name value
*
* @return string
2015-07-24 04:37:48 +02:00
*/
protected function generateName() {
do {
// Generate a new random name after this class
$class = get_class($this);
$entropy = substr(sha1(uniqid()), 0, 5);
$name = "{$class}_{$entropy}";
// Check if it conflicts
$exists = EditableFormField::get()->filter('Name', $name)->count() > 0;
} while($exists);
return $name;
}
2015-08-11 22:57:19 +02:00
/**
* Flag indicating that this field will set its own error message via data-msg='' attributes
2015-08-11 22:57:19 +02:00
*
* @return bool
*/
public function getSetsOwnError() {
return false;
}
2015-08-11 22:57:19 +02:00
/**
* Return whether a user can delete this form field
* based on whether they can edit the page
*
* @param Member $member
* @return bool
*/
public function canDelete($member = null) {
return $this->canEdit($member);
}
/**
* Return whether a user can edit this form field
* based on whether they can edit the page
*
* @param Member $member
* @return bool
*/
public function canEdit($member = null) {
$parent = $this->Parent();
if($parent && $parent->exists()) {
return $parent->canEdit($member) && !$this->isReadonly();
} else if (!$this->exists() && Controller::has_curr()) {
// This is for GridFieldOrderableRows support as it checks edit permissions on
// singleton of the class. Allows editing of User Defined Form pages by
// 'Content Authors' and those with permission to edit the UDF page. (ie. CanEditType/EditorGroups)
// This is to restore User Forms 2.x backwards compatibility.
$controller = Controller::curr();
if ($controller && $controller instanceof CMSPageEditController)
{
$parent = $controller->getRecord($controller->currentPageID());
// Only allow this behaviour on pages using UserFormFieldEditorExtension, such
// as UserDefinedForm page type.
if ($parent && $parent->hasExtension('UserFormFieldEditorExtension'))
{
return $parent->canEdit($member);
}
}
}
// Fallback to secure admin permissions
return parent::canEdit($member);
}
/**
* Return whether a user can view this form field
* based on whether they can view the page, regardless of the ReadOnly status of the field
*
* @param Member $member
* @return bool
*/
public function canView($member = null) {
$parent = $this->Parent();
if($parent && $parent->exists()) {
return $parent->canView($member);
2015-07-24 04:37:48 +02:00
}
return true;
}
2015-08-11 22:57:19 +02:00
/**
* Return whether a user can create an object of this type
*
* @param Member $member
* @param array $context Virtual parameter to allow context to be passed in to check
* @return bool
*/
public function canCreate($member = null) {
// Check parent page
$parent = $this->getCanCreateContext(func_get_args());
if($parent) {
return $parent->canEdit($member);
}
// Fall back to secure admin permissions
return parent::canCreate($member);
}
2015-08-11 22:57:19 +02:00
/**
* Helper method to check the parent for this object
*
* @param array $args List of arguments passed to canCreate
* @return SiteTree Parent page instance
*/
protected function getCanCreateContext($args) {
// Inspect second parameter to canCreate for a 'Parent' context
if(isset($args[1]['Parent'])) {
return $args[1]['Parent'];
}
// Hack in currently edited page if context is missing
if(Controller::has_curr() && Controller::curr() instanceof CMSMain) {
return Controller::curr()->currentPage();
}
// No page being edited
return null;
}
/**
* Check if can publish
*
* @param Member $member
* @return bool
*/
public function canPublish($member = null) {
return $this->canEdit($member);
}
/**
* Check if can unpublish
*
* @param Member $member
* @return bool
*/
public function canUnpublish($member = null) {
return $this->canDelete($member);
}
/**
* Publish this Form Field to the live site
2015-08-11 22:57:19 +02:00
*
* Wrapper for the {@link Versioned} publish function
*/
public function doPublish($fromStage, $toStage, $createNewVersion = false) {
$this->publish($fromStage, $toStage, $createNewVersion);
2015-07-24 04:37:48 +02:00
// Don't forget to publish the related custom rules...
foreach ($this->DisplayRules() as $rule) {
$rule->doPublish($fromStage, $toStage, $createNewVersion);
}
}
2015-08-11 22:57:19 +02:00
/**
2015-09-18 01:07:58 +02:00
* Delete this field from a given stage
*
* Wrapper for the {@link Versioned} deleteFromStage function
*/
public function doDeleteFromStage($stage) {
2015-09-18 01:07:58 +02:00
// Remove custom rules in this stage
$rules = Versioned::get_by_stage('EditableCustomRule', $stage)
->filter('ParentID', $this->ID);
foreach ($rules as $rule) {
2015-07-24 04:37:48 +02:00
$rule->deleteFromStage($stage);
}
2015-09-18 01:07:58 +02:00
// Remove record
$this->deleteFromStage($stage);
}
2015-08-11 22:57:19 +02:00
/**
* checks wether record is new, copied from Sitetree
*/
function isNew() {
if(empty($this->ID)) return true;
if(is_numeric($this->ID)) return false;
return stripos($this->ID, 'new') === 0;
}
/**
* checks if records is changed on stage
* @return boolean
*/
public function getIsModifiedOnStage() {
// new unsaved fields could be never be published
if($this->isNew()) return false;
$stageVersion = Versioned::get_versionnumber_by_stage('EditableFormField', 'Stage', $this->ID);
2015-07-24 04:37:48 +02:00
$liveVersion = Versioned::get_versionnumber_by_stage('EditableFormField', 'Live', $this->ID);
return ($stageVersion && $stageVersion != $liveVersion);
}
2015-08-11 22:57:19 +02:00
/**
2015-07-24 04:37:48 +02:00
* @deprecated since version 4.0
*/
public function getSettings() {
2015-07-24 04:37:48 +02:00
Deprecation::notice('4.0', 'getSettings is deprecated');
return (!empty($this->CustomSettings)) ? unserialize($this->CustomSettings) : array();
}
2015-08-11 22:57:19 +02:00
/**
2015-07-24 04:37:48 +02:00
* @deprecated since version 4.0
*/
public function setSettings($settings = array()) {
2015-07-24 04:37:48 +02:00
Deprecation::notice('4.0', 'setSettings is deprecated');
$this->CustomSettings = serialize($settings);
}
2015-08-11 22:57:19 +02:00
/**
2015-07-24 04:37:48 +02:00
* @deprecated since version 4.0
*/
public function setSetting($key, $value) {
2015-07-24 04:37:48 +02:00
Deprecation::notice('4.0', "setSetting({$key}) is deprecated");
$settings = $this->getSettings();
$settings[$key] = $value;
2015-08-11 22:57:19 +02:00
$this->setSettings($settings);
}
/**
* Set the allowed css classes for the extraClass custom setting
2015-08-11 22:57:19 +02:00
*
* @param array The permissible CSS classes to add
*/
public function setAllowedCss(array $allowed) {
2014-07-06 07:50:21 +02:00
if (is_array($allowed)) {
foreach ($allowed as $k => $v) {
self::$allowed_css[$k] = (!is_null($v)) ? $v : $k;
}
}
}
/**
2015-07-24 04:37:48 +02:00
* @deprecated since version 4.0
*/
public function getSetting($setting) {
2015-07-24 04:37:48 +02:00
Deprecation::notice("4.0", "getSetting({$setting}) is deprecated");
$settings = $this->getSettings();
if(isset($settings) && count($settings) > 0) {
if(isset($settings[$setting])) {
return $settings[$setting];
}
}
return '';
}
2015-08-11 22:57:19 +02:00
/**
* Get the path to the icon for this field type, relative to the site root.
*
* @return string
*/
public function getIcon() {
return USERFORMS_DIR . '/images/' . strtolower($this->class) . '.png';
}
2015-08-11 22:57:19 +02:00
/**
* Return whether or not this field has addable options
* such as a dropdown field or radio set
*
* @return bool
*/
public function getHasAddableOptions() {
return false;
}
2015-08-11 22:57:19 +02:00
/**
* Return whether or not this field needs to show the extra
* options dropdown list
2015-08-11 22:57:19 +02:00
*
* @return bool
*/
public function showExtraOptions() {
return true;
}
2015-07-24 04:37:48 +02:00
/**
* Returns the Title for rendering in the front-end (with XML values escaped)
*
* @return string
*/
public function getEscapedTitle() {
return Convert::raw2xml($this->Title);
}
2015-08-14 04:51:42 +02:00
/**
* Find the numeric indicator (1.1.2) that represents it's nesting value
*
* Only useful for fields attached to a current page, and that contain other fields such as pages
* or groups
*
* @return string
*/
public function getFieldNumber() {
// Check if exists
if(!$this->exists()) {
return null;
}
// Check parent
$form = $this->Parent();
if(!$form || !$form->exists() || !($fields = $form->Fields())) {
return null;
}
2015-09-11 00:20:06 +02:00
2015-08-14 04:51:42 +02:00
$prior = 0; // Number of prior group at this level
$stack = array(); // Current stack of nested groups, where the top level = the page
foreach($fields->map('ID', 'ClassName') as $id => $className) {
if($className === 'EditableFormStep') {
$priorPage = empty($stack) ? $prior : $stack[0];
$stack = array($priorPage + 1);
$prior = 0;
} elseif($className === 'EditableFieldGroup') {
$stack[] = $prior + 1;
$prior = 0;
} elseif($className === 'EditableFieldGroupEnd') {
$prior = array_pop($stack);
}
if($id == $this->ID) {
return implode('.', $stack);
}
}
return null;
}
public function getCMSTitle() {
return $this->i18n_singular_name() . ' (' . $this->Title . ')';
}
/**
2015-07-24 04:37:48 +02:00
* @deprecated since version 4.0
*/
public function getFieldName($field = false) {
2015-07-24 04:37:48 +02:00
Deprecation::notice('4.0', "getFieldName({$field}) is deprecated");
return ($field) ? "Fields[".$this->ID."][".$field."]" : "Fields[".$this->ID."]";
}
2015-08-11 22:57:19 +02:00
/**
2015-07-24 04:37:48 +02:00
* @deprecated since version 4.0
*/
public function getSettingName($field) {
2015-07-24 04:37:48 +02:00
Deprecation::notice('4.0', "getSettingName({$field}) is deprecated");
$name = $this->getFieldName('CustomSettings');
2015-08-11 22:57:19 +02:00
return $name . '[' . $field .']';
}
2015-08-11 22:57:19 +02:00
/**
2015-08-11 22:57:19 +02:00
* Append custom validation fields to the default 'Validation'
* section in the editable options view
2015-08-11 22:57:19 +02:00
*
2015-07-24 04:37:48 +02:00
* @return FieldList
*/
public function getFieldValidationOptions() {
$fields = new FieldList(
2015-08-17 00:43:51 +02:00
CheckboxField::create('Required', _t('EditableFormField.REQUIRED', 'Is this field Required?'))
->setDescription(_t('EditableFormField.REQUIRED_DESCRIPTION', 'Please note that conditional fields can\'t be required')),
2015-07-24 04:37:48 +02:00
TextField::create('CustomErrorMessage', _t('EditableFormField.CUSTOMERROR','Custom Error Message'))
);
$this->extend('updateFieldValidationOptions', $fields);
2015-08-11 22:57:19 +02:00
return $fields;
}
2015-08-11 22:57:19 +02:00
/**
2015-08-11 22:57:19 +02:00
* Return a FormField to appear on the front end. Implement on
* your subclass.
*
* @return FormField
*/
public function getFormField() {
user_error("Please implement a getFormField() on your EditableFormClass ". $this->ClassName, E_USER_ERROR);
}
/**
* Updates a formfield with extensions
*
* @param FormField $field
*/
public function doUpdateFormField($field) {
$this->extend('beforeUpdateFormField', $field);
$this->updateFormField($field);
$this->extend('afterUpdateFormField', $field);
}
/**
* Updates a formfield with the additional metadata specified by this field
*
* @param FormField $field
*/
protected function updateFormField($field) {
// set the error / formatting messages
$field->setCustomValidationMessage($this->getErrorMessage()->RAW());
// set the right title on this field
if($this->RightTitle) {
// Since this field expects raw html, safely escape the user data prior
$field->setRightTitle(Convert::raw2xml($this->RightTitle));
}
// if this field is required add some
if($this->Required) {
// Required validation can conflict so add the Required validation messages as input attributes
$errorMessage = $this->getErrorMessage()->HTML();
$field->addExtraClass('requiredField');
$field->setAttribute('data-rule-required', 'true');
$field->setAttribute('data-msg-required', $errorMessage);
if($identifier = UserDefinedForm::config()->required_identifier) {
$title = $field->Title() . " <span class='required-identifier'>". $identifier . "</span>";
$field->setTitle($title);
}
}
2015-08-11 22:57:19 +02:00
// if this field has an extra class
2015-08-14 04:51:42 +02:00
if($this->ExtraClass) {
$field->addExtraClass($this->ExtraClass);
}
}
2015-08-11 22:57:19 +02:00
/**
* Return the instance of the submission field class
*
* @return SubmittedFormField
*/
public function getSubmittedFormField() {
return new SubmittedFormField();
}
2015-08-11 22:57:19 +02:00
/**
* Show this form field (and its related value) in the reports and in emails.
*
* @return bool
*/
public function showInReports() {
return true;
}
2015-08-11 01:40:37 +02:00
/**
* Return the error message for this field. Either uses the custom
* one (if provided) or the default SilverStripe message
*
* @return Varchar
*/
public function getErrorMessage() {
$title = strip_tags("'". ($this->Title ? $this->Title : $this->Name) . "'");
$standard = sprintf(_t('Form.FIELDISREQUIRED', '%s is required').'.', $title);
2015-08-11 22:57:19 +02:00
// only use CustomErrorMessage if it has a non empty value
$errorMessage = (!empty($this->CustomErrorMessage)) ? $this->CustomErrorMessage : $standard;
2015-08-11 22:57:19 +02:00
return DBField::create_field('Varchar', $errorMessage);
}
2015-07-24 04:37:48 +02:00
/**
* Invoked by UserFormUpgradeService to migrate settings specific to this field from CustomSettings
* to the field proper
*
* @param array $data Unserialised data
*/
public function migrateSettings($data) {
// Map 'Show' / 'Hide' to boolean
if(isset($data['ShowOnLoad'])) {
$this->ShowOnLoad = $data['ShowOnLoad'] === '' || ($data['ShowOnLoad'] && $data['ShowOnLoad'] !== 'Hide');
unset($data['ShowOnLoad']);
}
2015-08-11 22:57:19 +02:00
2015-07-24 04:37:48 +02:00
// Migrate all other settings
foreach($data as $key => $value) {
if($this->hasField($key)) {
$this->setField($key, $value);
}
}
}
/**
* Get the formfield to use when editing this inline in gridfield
*
* @param string $column name of column
* @param array $fieldClasses List of allowed classnames if this formfield has a selectable class
* @return FormField
*/
public function getInlineClassnameField($column, $fieldClasses) {
return DropdownField::create($column, false, $fieldClasses);
}
/**
* Get the formfield to use when editing the title inline
*
* @param string $column
* @return FormField
*/
public function getInlineTitleField($column) {
return TextField::create($column, false)
->setAttribute('placeholder', _t('EditableFormField.TITLE', 'Title'))
->setAttribute('data-placeholder', _t('EditableFormField.TITLE', 'Title'));
}
/**
* Get the JS expression for selecting the holder for this field
*
* @return string
*/
public function getSelectorHolder() {
return "$(\"#{$this->Name}\")";
}
/**
* Gets the JS expression for selecting the value for this field
*
* @param EditableCustomRule $rule Custom rule this selector will be used with
* @param bool $forOnLoad Set to true if this will be invoked on load
*/
public function getSelectorField(EditableCustomRule $rule, $forOnLoad = false) {
return "$(\"input[name='{$this->Name}']\")";
}
/**
* Get the list of classes that can be selected and used as data-values
*
* @param $includeLiterals Set to false to exclude non-data fields
* @return array
*/
public function getEditableFieldClasses($includeLiterals = true) {
$classes = ClassInfo::getValidSubClasses('EditableFormField');
// Remove classes we don't want to display in the dropdown.
$editableFieldClasses = array();
foreach ($classes as $class) {
// Skip abstract / hidden classes
if(Config::inst()->get($class, 'abstract', Config::UNINHERITED) || Config::inst()->get($class, 'hidden')
) {
continue;
}
if(!$includeLiterals && Config::inst()->get($class, 'literal')) {
continue;
}
$singleton = singleton($class);
if(!$singleton->canCreate()) {
continue;
}
$editableFieldClasses[$class] = $singleton->i18n_singular_name();
}
asort($editableFieldClasses);
return $editableFieldClasses;
}
2015-08-17 00:43:51 +02:00
/**
* @return EditableFormFieldValidator
*/
public function getCMSValidator() {
return EditableFormFieldValidator::create()
->setRecord($this);
}
/**
* Determine effective display rules for this field.
*
* @return SS_List
*/
public function EffectiveDisplayRules() {
if($this->Required) {
return new ArrayList();
}
return $this->DisplayRules();
}
2010-11-02 21:14:28 +01:00
}