Version: current

     1 <?php
     2 /**
     3  * Read-only complement of {@link DropdownField}.
     4  * Shows the "human value" of the dropdown field for the currently selected value.
     5  * @package forms
     6  * @subpackage fields-basic
     7  */
     8 class LookupField extends DropdownField {
     9
    10     protected $readonly = true;
    11
    12     /**
    13      * Returns a readonly span containing the correct value.
    14      */
    15     function Field() {
    16
    17         if(trim($this->value) || $this->value === '0') {
    18             $this->value = trim($this->value);
    19             $source = $this->getSource();
    20             if(is_array($source)) {
    21                 $mappedValue = isset($source[$this->value]) ? $source[$this->value] : null;
    22             } elseif($source instanceof SQLMap) {
    23                 $mappedValue = $source->getItem($this->value);
    24             }
    25         }
    26
    27         if(!isset($mappedValue)) $mappedValue = "<i>(none)</i>";
    28
    29         if($this->value) {
    30             $val = $this->dontEscape
    31                 ? ($this->reserveNL?Convert::raw2xml($this->value):$this->value)
    32                 : Convert::raw2xml($this->value);
    33         } else {
    34             $val = '<i>(none)</i>';
    35         }
    36
    37         $valforInput = $this->value ? Convert::raw2att($val) : "";
    38
    39         return "<span class=\"readonly\" id=\"" . $this->id() .
    40             "\">$mappedValue</span><input type=\"hidden\" name=\"" . $this->name .
    41             "\" value=\"" . $valforInput . "\" />";
    42     }
    43
    44     function performReadonlyTransformation() {
    45         $clone = clone $this;
    46         return $clone;
    47     }
    48
    49     function Type() {
    50         return "lookup readonly";
    51     }
    52
    53     /**
    54      * Override parent behaviour by not merging arrays.
    55      */
    56     function getSource() {
    57         return $this->source;
    58     }
    59 }
    60
    61 ?>

[Raise a SilverStripe Framework issue/bug](https://github.com/silverstripe/silverstripe-framework/issues/new)
- [Raise a SilverStripe CMS issue/bug](https://github.com/silverstripe/silverstripe-cms/issues/new)
- Please use the Silverstripe Forums to ask development related questions. -