public function transform(FormField $field) { // Look for a performXXTransformation() method on the field itself. // performReadonlyTransformation() is a pretty commonly applied method. // Otherwise, look for a transformXXXField() method on this object. // This is more commonly done in custom transformations // We iterate through each array simultaneously, looking at [0] of both, then [1] of both. // This provides a more natural failover scheme. $transNames = array_reverse(array_map(function ($name) { return ClassInfo::shortName($name); }, array_values(ClassInfo::ancestry($this->class)))); $fieldClasses = array_reverse(array_map(function ($name) { return ClassInfo::shortName($name); }, array_values(ClassInfo::ancestry($field->class)))); $len = max(sizeof($transNames), sizeof($fieldClasses)); for ($i = 0; $i < $len; $i++) { // This is lets fieldClasses be longer than transNames if (!empty($transNames[$i])) { $funcName = 'perform' . $transNames[$i]; if ($field->hasMethod($funcName)) { //echo "<li>$field->class used $funcName"; return $field->{$funcName}($this); } } // And this one does the reverse. if (!empty($fieldClasses[$i])) { $funcName = 'transform' . $fieldClasses[$i]; if ($this->hasMethod($funcName)) { //echo "<li>$field->class used $funcName"; return $this->{$funcName}($field); } } } throw new \BadMethodCallException("FormTransformation:: Can't perform '{$this->class}' on '{$field->class}'"); }
/** * Generate the field ID value * * @param FormField $field * @return string */ public function generateFieldID($field) { if ($form = $field->getForm()) { return sprintf("%s_%s", $this->generateFormID($form), Convert::raw2htmlid($field->getName())); } return Convert::raw2htmlid($field->getName()); }
public function getSchemaStateDefaults() { $state = parent::getSchemaStateDefaults(); $state['data']['files'] = $this->getEncodedItems(); $state['value'] = $this->Value() ?: ['Files' => []]; return $state; }
/** * Returns the root field set that this belongs to * * @return FieldList|FormField */ public function rootFieldList() { if ($this->containerField) { return $this->containerField->rootFieldList(); } return $this; }
/** * @return array */ public function getAttributes() { $maxLength = $this->getMaxLength(); $attributes = array(); if ($maxLength) { $attributes['maxLength'] = $maxLength; $attributes['size'] = min($maxLength, 30); } return array_merge(parent::getAttributes(), $attributes); }
public function getSchemaStateDefaults() { $state = parent::getSchemaStateDefaults(); if ($record = $this->getRecord()) { $latest = Versioned::get_latest_version($record->baseClass(), $record->ID); if ($latest) { $state['data']['fileId'] = $latest->ID; $state['data']['latestVersionId'] = $latest->Version; } } return $state; }
/** * @param String $name * @param String $title * @param String $managedClass * @param String $filterField * @param Group|SS_List $records One or more {@link Group} or {@link PermissionRole} records * used to determine permission checkboxes. * Caution: saveInto() can only be used with a single record, all inherited permissions will be marked readonly. * Setting multiple groups only makes sense in a readonly context. (Optional) */ public function __construct($name, $title, $managedClass, $filterField, $records = null) { $this->filterField = $filterField; $this->managedClass = $managedClass; if ($records instanceof SS_List) { $this->records = $records; } elseif ($records instanceof Group) { $this->records = new ArrayList(array($records)); } elseif ($records) { throw new InvalidArgumentException('$record should be either a Group record, or a SS_List of Group records'); } // Get all available codes in the system as a categorized nested array $this->source = Permission::get_codes(true); parent::__construct($name, $title); }
public function FieldList() { $items = parent::FieldList()->toArray(); $count = 0; $newItems = array(); /** @var SelectionGroup_Item $item */ foreach ($items as $item) { if ($this->value == $item->getValue()) { $firstSelected = true; $checked = true; } else { $firstSelected = false; $checked = false; } $itemID = $this->ID() . '_' . ++$count; // @todo Move into SelectionGroup_Item.ss template at some point. $extra = array("RadioButton" => DBField::create_field('HTMLFragment', FormField::create_tag('input', array('class' => 'selector', 'type' => 'radio', 'id' => $itemID, 'name' => $this->name, 'value' => $item->getValue(), 'checked' => $checked, 'aria-labelledby' => "title-{$itemID}"))), "RadioLabel" => DBField::create_field('HTMLFragment', FormField::create_tag('label', array('id' => "title-{$itemID}", 'for' => $itemID), $item->getTitle())), "Selected" => $firstSelected); $newItems[] = $item->customise($extra); } return new ArrayList($newItems); }
public function getHTMLFragments($gridField) { $modelClass = $gridField->getModelClass(); $parentID = 0; if (!$this->currentID) { return null; } $modelObj = DataObject::get_by_id($modelClass, $this->currentID); $parent = null; if ($modelObj->hasMethod('getParent')) { $parent = $modelObj->getParent(); } elseif ($modelObj->ParentID) { $parent = $modelObj->Parent(); } if ($parent) { $parentID = $parent->ID; } // Attributes $attrs = array_merge($this->attributes, array('href' => sprintf($this->linkSpec, $parentID), 'class' => 'cms-panel-link ss-ui-button font-icon-level-up no-text grid-levelup')); $linkTag = FormField::create_tag('a', $attrs); $forTemplate = new ArrayData(array('UpLink' => DBField::create_field('HTMLFragment', $linkTag))); $template = SSViewer::get_templates_by_class($this, '', __CLASS__); return array('before' => $forTemplate->renderWith($template)); }
public function testGetSchemaStateWithFormValidation() { $field = new FormField('MyField', 'My Field'); $validator = new RequiredFields('MyField'); $form = new Form(new Controller(), 'TestForm', new FieldList($field), new FieldList(), $validator); $form->validate(); $form->setupFormErrors(); $schema = $field->getSchemaState(); $this->assertEquals(['html' => '"My Field" is required'], $schema['message']['value']); }
/** * @param array $properties * @return string */ public function Field($properties = array()) { return FormField::create_tag('input', array('type' => 'submit', 'name' => sprintf('action_%s', $this->getName()), 'value' => $this->title, 'id' => $this->ID(), 'class' => sprintf('action%s', $this->extraClass))); }
/** * We overwrite the field attribute to add our hidden fields, as this * formfield can contain multiple values. * * @param array $properties * @return DBHTMLText */ public function Field($properties = array()) { $value = ''; $titleArray = array(); $idArray = array(); $items = $this->getItems(); $emptyTitle = _t('DropdownField.CHOOSE', '(Choose)', 'start value of a dropdown'); if ($items && count($items)) { foreach ($items as $item) { $idArray[] = $item->ID; $titleArray[] = $item instanceof ViewableData ? $item->obj($this->labelField)->forTemplate() : Convert::raw2xml($item->{$this->labelField}); } $title = implode(", ", $titleArray); $value = implode(",", $idArray); } else { $title = $emptyTitle; } $dataUrlTree = ''; if ($this->form) { $dataUrlTree = $this->Link('tree'); if (!empty($idArray)) { $dataUrlTree = Controller::join_links($dataUrlTree, '?forceValue=' . implode(',', $idArray)); } } $properties = array_merge($properties, array('Title' => $title, 'EmptyTitle' => $emptyTitle, 'Link' => $dataUrlTree, 'Value' => $value)); return FormField::Field($properties); }
/** * Inserts a field after a particular field in a FieldList. * * @param string $insertAfter Name of the field to insert after * @param FormField $field The form field to insert * @return FormField|null */ public function insertAfter($insertAfter, $field) { if ($field instanceof Tab || $field instanceof TabSet) { $field->setTabSet($this); } return parent::insertAfter($insertAfter, $field); }
/** * Returns another instance of this field, but "cast" to a different class. * * @see FormField::castedCopy() * * @param String $classOrCopy * @return FormField */ public function castedCopy($classOrCopy) { $field = parent::castedCopy($classOrCopy); if ($field instanceof SelectField) { $field->setSource($this->getSource()); } return $field; }
/** * Returns a <select> tag containing all the appropriate <option> tags * * @param array $properties * @return string */ public function Field($properties = array()) { $properties = array_merge($properties, array('Options' => $this->getOptions())); return FormField::Field($properties); }
public function Field($properties = array()) { $options = array(); $odd = false; // Add all options striped foreach ($this->getSourceEmpty() as $value => $title) { $odd = !$odd; $options[] = $this->getFieldOption($value, $title, $odd); } $properties = array_merge($properties, array('Options' => new ArrayList($options))); return FormField::Field($properties); }
/** * Only save if field was shown on the client, and is not empty. * * @param DataObjectInterface $record */ public function saveInto(DataObjectInterface $record) { if (!$this->isSaveable()) { return; } if (!($this->canBeEmpty && !$this->value)) { parent::saveInto($record); } }
/** * Get a human-readable label for a single field, * see {@link fieldLabels()} for more details. * * @uses fieldLabels() * @uses FormField::name_to_label() * * @param string $name Name of the field * @return string Label of the field */ public function fieldLabel($name) { $labels = $this->fieldLabels(); return isset($labels[$name]) ? $labels[$name] : FormField::name_to_label($name); }
public function setForm($form) { $this->fieldCurrency->setForm($form); $this->fieldAmount->setForm($form); return parent::setForm($form); }
/** * @param $content * * @return string */ protected function getOptionalTableFooter($content) { if ($content['footer']) { return FormField::create_tag('tfoot', array(), $content['footer']); } return ''; }
public function extraClass() { return implode(' ', array(parent::extraClass(), $this->showSearch ? "searchable" : null)); }
public function setReadonly($readonly) { parent::setReadonly($readonly); foreach ($this->getChildren() as $child) { $child->setReadonly($readonly); } return $this; }
public function getAttributes() { $type = isset($this->attributes['src']) ? 'image' : 'submit'; return array_merge(parent::getAttributes(), array('disabled' => $this->isReadonly() || $this->isDisabled(), 'value' => $this->Title(), 'type' => $type, 'title' => $this->useButtonTag ? $this->description : null)); }
public function getAttributes() { return array_merge(parent::getAttributes(), array('type' => 'file')); }
/** * @param FormField $field */ public function setTimeField($field) { $expected = $this->getName() . '[time]'; if ($field->getName() != $expected) { throw new InvalidArgumentException(sprintf('Wrong name format for time field: "%s" (expected "%s")', $field->getName(), $expected)); } $field->setForm($this->getForm()); $this->timeField = $field; $this->setValue($this->value); // update value }