silverstripe-framework/forms/LookupField.php
Damian Mooyman 5c9044a007 API Enforce default_cast for all field usages
API Introduce HTMLFragment as casting helper for HTMLText with shortcodes disabled
API Introduce DBField::CDATA for XML file value encoding
API RSSFeed now casts from the underlying model rather than by override
API Introduce CustomMethods::getExtraMethodConfig() to allow metadata to be queried
BUG Remove _call hack from VirtualPage
API Remove FormField::$dontEscape
API Introduce HTMLReadonlyField for non-editable readonly HTML
API FormField::Field() now returns string in many cases rather than DBField instance.
API Remove redundant *_val methods from ViewableData
API ViewableData::obj() no longer has a $forceReturnObject parameter as it always returns an object
BUG  Fix issue with ViewableData caching incorrect field values after being modified.
API Remove deprecated DB class methods
API Enforce plain text left/right formfield titles
2016-07-13 17:15:45 +12:00

104 lines
2.2 KiB
PHP

<?php
use SilverStripe\Model\FieldType\DBField;
use SilverStripe\ORM\DataObjectInterface;
/**
* Read-only complement of {@link DropdownField}.
*
* Shows the "human value" of the dropdown field for the currently selected
* value.
*
* @package forms
* @subpackage fields-basic
*/
class LookupField extends MultiSelectField {
/**
* @var boolean $readonly
*/
protected $readonly = true;
/**
* Returns a readonly span containing the correct value.
*
* @param array $properties
*
* @return string
*/
public function Field($properties = array()) {
$source = ArrayLib::flatten($this->getSource());
$values = $this->getValueArray();
// Get selected values
$mapped = array();
foreach($values as $value) {
if(isset($source[$value])) {
$mapped[] = $source[$value];
}
}
// Don't check if string arguments are matching against the source,
// as they might be generated HTML diff views instead of the actual values
if($this->value && is_string($this->value) && empty($mapped)) {
$mapped = array(trim($this->value));
$values = array();
}
if($mapped) {
$attrValue = implode(', ', array_values($mapped));
$attrValue = Convert::raw2xml($attrValue);
$inputValue = implode(', ', array_values($values));
} else {
$attrValue = '<i>('._t('FormField.NONE', 'none').')</i>';
$inputValue = '';
}
$properties = array_merge($properties, array(
'AttrValue' => DBField::create_field('HTMLFragment', $attrValue),
'InputValue' => $inputValue
));
return parent::Field($properties);
}
/**
* Ignore validation as the field is readonly
*
* @param Validator $validator
* @return bool
*/
public function validate($validator) {
return true;
}
/**
* Stubbed so invalid data doesn't save into the DB
*
* @param DataObjectInterface $record DataObject to save data into
*/
public function saveInto(DataObjectInterface $record) {
}
/**
* @return LookupField
*/
public function performReadonlyTransformation() {
$clone = clone $this;
return $clone;
}
public function getHasEmptyDefault() {
return false;
}
/**
* @return string
*/
public function Type() {
return "lookup readonly";
}
}