From 8b0bb8dd09b6a7407be8a754c74e4475fbcd9702 Mon Sep 17 00:00:00 2001 From: Sean Harvey Date: Thu, 15 Nov 2012 14:19:57 +1300 Subject: [PATCH] API Replace deprecated FormField::createTag() with static create_tag() GridField uses createTag() which is marked for deprecation, rather than have it used as the cornerstone of generating FormField templates, use it as a helper in case fields generate HTML tags from PHP. --- forms/AjaxUniqueTextField.php | 2 +- forms/FormField.php | 38 +++++++++++------------ forms/GroupedDropdownField.php | 2 +- forms/HtmlEditorField.php | 9 +++--- forms/TreeMultiselectField.php | 4 +-- forms/gridfield/GridField.php | 23 +++++++------- forms/gridfield/GridFieldDeleteAction.php | 2 +- forms/gridfield/GridFieldEditButton.php | 2 +- 8 files changed, 41 insertions(+), 41 deletions(-) diff --git a/forms/AjaxUniqueTextField.php b/forms/AjaxUniqueTextField.php index 952f507f5..d41fed224 100644 --- a/forms/AjaxUniqueTextField.php +++ b/forms/AjaxUniqueTextField.php @@ -52,7 +52,7 @@ class AjaxUniqueTextField extends TextField { 'maxlength' => ($this->maxLength) ? $this->maxLength : null ); - return $this->createTag('input', $attributes); + return FormField::create_tag('input', $attributes); } public function validate( $validator ) { diff --git a/forms/FormField.php b/forms/FormField.php index 6f2670ba6..0ec3dd06a 100644 --- a/forms/FormField.php +++ b/forms/FormField.php @@ -117,6 +117,23 @@ class FormField extends RequestHandler { return $label; } + /** + * Construct and return HTML tag. + */ + public static function create_tag($tag, $attributes, $content = null) { + $preparedAttributes = ''; + foreach($attributes as $k => $v) { + // Note: as indicated by the $k == value item here; the decisions over what to include in the attributes + // can sometimes get finicky + if(!empty($v) || $v === '0' || $k == 'value') { + $preparedAttributes .= " $k=\"" . Convert::raw2att($v) . "\""; + } + } + + if($content || $tag != 'input') return "<$tag$preparedAttributes>$content"; + else return "<$tag$preparedAttributes />"; + } + /** * Create a new field. * @param name The internal field name, passed to forms. @@ -739,26 +756,9 @@ class FormField extends RequestHandler { return strtolower(preg_replace('/Field$/', '', $this->class)); } - /** - * Construct and return HTML tag. - * - * @deprecated 3.0 Please define your own FormField template using {@link setFieldTemplate()} - * and/or {@link renderFieldTemplate()} - * - * @todo Transform to static helper method. - */ public function createTag($tag, $attributes, $content = null) { - $preparedAttributes = ''; - foreach($attributes as $k => $v) { - // Note: as indicated by the $k == value item here; the decisions over what to include in the attributes - // can sometimes get finicky - if(!empty($v) || $v === '0' || $k == 'value') { - $preparedAttributes .= " $k=\"" . Convert::raw2att($v) . "\""; - } - } - - if($content || $tag != 'input') return "<$tag$preparedAttributes>$content"; - else return "<$tag$preparedAttributes />"; + Deprecation::notice('3.1', 'Use FormField::create_tag()'); + return self::create_tag($tag, $attributes, $content); } /** diff --git a/forms/GroupedDropdownField.php b/forms/GroupedDropdownField.php index 50e2ae724..3ab4bab5b 100644 --- a/forms/GroupedDropdownField.php +++ b/forms/GroupedDropdownField.php @@ -55,7 +55,7 @@ class GroupedDropdownField extends DropdownField { } } - return $this->createTag('select', $this->getAttributes(), $options); + return FormField::create_tag('select', $this->getAttributes(), $options); } public function Type() { diff --git a/forms/HtmlEditorField.php b/forms/HtmlEditorField.php index c266b2d0e..deee5bccd 100644 --- a/forms/HtmlEditorField.php +++ b/forms/HtmlEditorField.php @@ -76,11 +76,10 @@ class HtmlEditorField extends TextareaField { } } - return $this->createTag ( - 'textarea', - $this->getAttributes(), - htmlentities($value->getContent(), ENT_COMPAT, 'UTF-8') - ); + $properties['Value'] = htmlentities($value->getContent(), ENT_COMPAT, 'UTF-8'); + $obj = $this->customise($properties); + + return $obj->renderWith($this->getTemplates()); } public function getAttributes() { diff --git a/forms/TreeMultiselectField.php b/forms/TreeMultiselectField.php index 6a28273ba..f651e0ad2 100644 --- a/forms/TreeMultiselectField.php +++ b/forms/TreeMultiselectField.php @@ -115,7 +115,7 @@ class TreeMultiselectField extends TreeDropdownField { $title = _t('DropdownField.CHOOSE', '(Choose)', 'start value of a dropdown'); } - return $this->createTag ( + return FormField::create_tag( 'div', array ( 'id' => "TreeDropdownField_{$this->id()}", @@ -125,7 +125,7 @@ class TreeMultiselectField extends TreeDropdownField { 'data-title' => $title, 'title' => $this->getDescription() ), - $this->createTag ( + FormField::create_tag( 'input', array ( 'id' => $this->id(), diff --git a/forms/gridfield/GridField.php b/forms/gridfield/GridField.php index 16e1215af..4cbaec58e 100644 --- a/forms/gridfield/GridField.php +++ b/forms/gridfield/GridField.php @@ -339,13 +339,13 @@ class GridField extends FormField { // A return value of null means this columns should be skipped altogether. if($colContent === null) continue; $colAttributes = $this->getColumnAttributes($record, $column); - $rowContent .= $this->createTag('td', $colAttributes, $colContent); + $rowContent .= FormField::create_tag('td', $colAttributes, $colContent); } $classes = array('ss-gridfield-item'); if ($idx == 0) $classes[] = 'first'; if ($idx == $total-1) $classes[] = 'last'; $classes[] = ($idx % 2) ? 'even' : 'odd'; - $row = $this->createTag( + $row = FormField::create_tag( 'tr', array( "class" => implode(' ', $classes), @@ -361,26 +361,27 @@ class GridField extends FormField { } // Display a message when the grid field is empty - if(!(isset($content['body']) && $content['body'])) { - $content['body'] = $this->createTag( + if(!(isset($content['body']) && $content['body'])) { + $content['body'] = FormField::create_tag( 'tr', array("class" => 'ss-gridfield-item ss-gridfield-no-items'), - $this->createTag( + FormField::create_tag( 'td', array('colspan' => count($columns)), - _t('GridField.NoItemsFound', 'No items found')) + _t('GridField.NoItemsFound', 'No items found') + ) ); } // Turn into the relevant parts of a table $head = $content['header'] - ? $this->createTag('thead', array(), $content['header']) + ? FormField::create_tag('thead', array(), $content['header']) : ''; $body = $content['body'] - ? $this->createTag('tbody', array('class' => 'ss-gridfield-items'), $content['body']) + ? FormField::create_tag('tbody', array('class' => 'ss-gridfield-items'), $content['body']) : ''; $foot = $content['footer'] - ? $this->createTag('tfoot', array(), $content['footer']) + ? FormField::create_tag('tfoot', array(), $content['footer']) : ''; $this->addExtraClass('ss-gridfield field'); @@ -398,9 +399,9 @@ class GridField extends FormField { return - $this->createTag('fieldset', $attrs, + FormField::create_tag('fieldset', $attrs, $content['before'] . - $this->createTag('table', $tableAttrs, $head."\n".$foot."\n".$body) . + FormField::create_tag('table', $tableAttrs, $head."\n".$foot."\n".$body) . $content['after'] ); } diff --git a/forms/gridfield/GridFieldDeleteAction.php b/forms/gridfield/GridFieldDeleteAction.php index e5a959418..16c24a10b 100644 --- a/forms/gridfield/GridFieldDeleteAction.php +++ b/forms/gridfield/GridFieldDeleteAction.php @@ -45,7 +45,7 @@ class GridFieldDeleteAction implements GridField_ColumnProvider, GridField_Actio } /** - * Return any special attributes that will be used for FormField::createTag() + * Return any special attributes that will be used for FormField::create_tag() * * @param GridField $gridField * @param DataObject $record diff --git a/forms/gridfield/GridFieldEditButton.php b/forms/gridfield/GridFieldEditButton.php index 1bada543b..1c6836ed6 100644 --- a/forms/gridfield/GridFieldEditButton.php +++ b/forms/gridfield/GridFieldEditButton.php @@ -20,7 +20,7 @@ class GridFieldEditButton implements GridField_ColumnProvider { } /** - * Return any special attributes that will be used for FormField::createTag() + * Return any special attributes that will be used for FormField::create_tag() * * @param GridField $gridField * @param DataObject $record