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.
This commit is contained in:
Sean Harvey 2012-11-15 14:19:57 +13:00
parent 8c3ecabc31
commit 8b0bb8dd09
8 changed files with 41 additions and 41 deletions

View File

@ -52,7 +52,7 @@ class AjaxUniqueTextField extends TextField {
'maxlength' => ($this->maxLength) ? $this->maxLength : null 'maxlength' => ($this->maxLength) ? $this->maxLength : null
); );
return $this->createTag('input', $attributes); return FormField::create_tag('input', $attributes);
} }
public function validate( $validator ) { public function validate( $validator ) {

View File

@ -117,6 +117,23 @@ class FormField extends RequestHandler {
return $label; 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</$tag>";
else return "<$tag$preparedAttributes />";
}
/** /**
* Create a new field. * Create a new field.
* @param name The internal field name, passed to forms. * @param name The internal field name, passed to forms.
@ -739,26 +756,9 @@ class FormField extends RequestHandler {
return strtolower(preg_replace('/Field$/', '', $this->class)); 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) { public function createTag($tag, $attributes, $content = null) {
$preparedAttributes = ''; Deprecation::notice('3.1', 'Use FormField::create_tag()');
foreach($attributes as $k => $v) { return self::create_tag($tag, $attributes, $content);
// 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</$tag>";
else return "<$tag$preparedAttributes />";
} }
/** /**

View File

@ -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() { public function Type() {

View File

@ -76,11 +76,10 @@ class HtmlEditorField extends TextareaField {
} }
} }
return $this->createTag ( $properties['Value'] = htmlentities($value->getContent(), ENT_COMPAT, 'UTF-8');
'textarea', $obj = $this->customise($properties);
$this->getAttributes(),
htmlentities($value->getContent(), ENT_COMPAT, 'UTF-8') return $obj->renderWith($this->getTemplates());
);
} }
public function getAttributes() { public function getAttributes() {

View File

@ -115,7 +115,7 @@ class TreeMultiselectField extends TreeDropdownField {
$title = _t('DropdownField.CHOOSE', '(Choose)', 'start value of a dropdown'); $title = _t('DropdownField.CHOOSE', '(Choose)', 'start value of a dropdown');
} }
return $this->createTag ( return FormField::create_tag(
'div', 'div',
array ( array (
'id' => "TreeDropdownField_{$this->id()}", 'id' => "TreeDropdownField_{$this->id()}",
@ -125,7 +125,7 @@ class TreeMultiselectField extends TreeDropdownField {
'data-title' => $title, 'data-title' => $title,
'title' => $this->getDescription() 'title' => $this->getDescription()
), ),
$this->createTag ( FormField::create_tag(
'input', 'input',
array ( array (
'id' => $this->id(), 'id' => $this->id(),

View File

@ -339,13 +339,13 @@ class GridField extends FormField {
// A return value of null means this columns should be skipped altogether. // A return value of null means this columns should be skipped altogether.
if($colContent === null) continue; if($colContent === null) continue;
$colAttributes = $this->getColumnAttributes($record, $column); $colAttributes = $this->getColumnAttributes($record, $column);
$rowContent .= $this->createTag('td', $colAttributes, $colContent); $rowContent .= FormField::create_tag('td', $colAttributes, $colContent);
} }
$classes = array('ss-gridfield-item'); $classes = array('ss-gridfield-item');
if ($idx == 0) $classes[] = 'first'; if ($idx == 0) $classes[] = 'first';
if ($idx == $total-1) $classes[] = 'last'; if ($idx == $total-1) $classes[] = 'last';
$classes[] = ($idx % 2) ? 'even' : 'odd'; $classes[] = ($idx % 2) ? 'even' : 'odd';
$row = $this->createTag( $row = FormField::create_tag(
'tr', 'tr',
array( array(
"class" => implode(' ', $classes), "class" => implode(' ', $classes),
@ -362,25 +362,26 @@ class GridField extends FormField {
// Display a message when the grid field is empty // Display a message when the grid field is empty
if(!(isset($content['body']) && $content['body'])) { if(!(isset($content['body']) && $content['body'])) {
$content['body'] = $this->createTag( $content['body'] = FormField::create_tag(
'tr', 'tr',
array("class" => 'ss-gridfield-item ss-gridfield-no-items'), array("class" => 'ss-gridfield-item ss-gridfield-no-items'),
$this->createTag( FormField::create_tag(
'td', 'td',
array('colspan' => count($columns)), array('colspan' => count($columns)),
_t('GridField.NoItemsFound', 'No items found')) _t('GridField.NoItemsFound', 'No items found')
)
); );
} }
// Turn into the relevant parts of a table // Turn into the relevant parts of a table
$head = $content['header'] $head = $content['header']
? $this->createTag('thead', array(), $content['header']) ? FormField::create_tag('thead', array(), $content['header'])
: ''; : '';
$body = $content['body'] $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'] $foot = $content['footer']
? $this->createTag('tfoot', array(), $content['footer']) ? FormField::create_tag('tfoot', array(), $content['footer'])
: ''; : '';
$this->addExtraClass('ss-gridfield field'); $this->addExtraClass('ss-gridfield field');
@ -398,9 +399,9 @@ class GridField extends FormField {
return return
$this->createTag('fieldset', $attrs, FormField::create_tag('fieldset', $attrs,
$content['before'] . $content['before'] .
$this->createTag('table', $tableAttrs, $head."\n".$foot."\n".$body) . FormField::create_tag('table', $tableAttrs, $head."\n".$foot."\n".$body) .
$content['after'] $content['after']
); );
} }

View File

@ -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 GridField $gridField
* @param DataObject $record * @param DataObject $record

View File

@ -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 GridField $gridField
* @param DataObject $record * @param DataObject $record