Skip to content
Permalink
Browse files

render attributes based on data instead of view->vars so as to allow …

…overriding during rendering
  • Loading branch information...
flack committed Mar 25, 2019
1 parent f8c81e7 commit 3222b4a6aee0ef30db67caff79975cbd93ab3d73
@@ -63,10 +63,10 @@ public function form_rest(FormView $view, array $data)
public function widget_attributes(FormView $view, array $data)
{
$attributes = $view->vars['attr'];
$attributes = $data['attr'];
$attributes['id'] = $data['id'];
$attributes['name'] = $data['full_name'];
if (!empty($view->vars['readonly'])) {
if (!empty($data['readonly'])) {
$attributes['readonly'] = 'readonly';
}
if ($data['disabled']) {
@@ -72,7 +72,7 @@ public function autocomplete_widget(FormView $view, array $data)
public function choice_widget_collapsed(FormView $view, array $data)
{
if (isset($data['data'])) {
if (!empty($view->vars['multiple'])) {
if (!empty($data['multiple'])) {
$selection = $data['data'];
} else {
$selection = (string) $data['data'];
@@ -203,21 +203,20 @@ public function form_widget_simple(FormView $view, array $data)
{
$type = isset($data['type']) ? $data['type'] : 'text';
if ($view->vars['readonly'] && $type !== 'hidden') {
return $data['value'] . $this->renderer->block($view, 'form_widget_simple', ['type' => "hidden"]);
$data['type'] = 'hidden';
return $data['value'] . $this->renderer->block($view, 'form_widget_simple', $data);
}
if (!empty($data['attr']['class'])) {
$view->vars['attr']['class'] = $data['attr']['class'];
} elseif (in_array($type, ['text', 'password', 'email', 'url'])) {
$view->vars['attr']['class'] = 'shorttext';
if (empty($data['attr']['class']) && in_array($type, ['text', 'password', 'email', 'url'])) {
$data['attr']['class'] = 'shorttext';
}
if ( !empty($data['value'])
|| is_numeric($data['value'])) {
$view->vars['attr']['value'] = $data['value'];
$data['attr']['value'] = $data['value'];
}
$view->vars['attr']['type'] = $type;
return '<input ' . $this->renderer->block($view, 'widget_attributes') . ' />';
$data['attr']['type'] = $type;
return '<input ' . $this->renderer->block($view, 'widget_attributes', $data) . ' />';
}
public function button_widget(FormView $view, array $data)
@@ -263,25 +262,25 @@ public function autocomplete_widget(FormView $view, array $data)
return $string . $this->jsinit($jsinit);
}
protected function prepare_widget_attributes($type, FormView $view, array $data)
protected function prepare_widget_attributes($type, FormView $view, array &$data)
{
$view->vars['attr']['type'] = $type;
$data['attr']['type'] = $type;
if (isset($data['value'])) {
$view->vars['attr']['value'] = $data['value'];
$data['attr']['value'] = $data['value'];
}
if ($data['checked']) {
$view->vars['attr']['checked'] = 'checked';
$data['attr']['checked'] = 'checked';
}
}
public function radio_widget(FormView $view, array $data)
{
$this->prepare_widget_attributes('radio', $view, $data);
if ($view->vars['readonly']) {
$view->vars['attr']['disabled'] = true;
$data['attr']['disabled'] = true;
}
return '<input ' . $this->renderer->block($view, 'widget_attributes') . ' />';
return '<input ' . $this->renderer->block($view, 'widget_attributes', $data) . ' />';
}
public function checkbox_widget(FormView $view, array $data)
@@ -295,7 +294,7 @@ public function checkbox_widget(FormView $view, array $data)
}
$this->prepare_widget_attributes('checkbox', $view, $data);
return '<input ' . $this->renderer->block($view, 'widget_attributes') . ' />';
return '<input ' . $this->renderer->block($view, 'widget_attributes', $data) . ' />';
}
public function choice_widget_collapsed(FormView $view, array $data)
@@ -313,10 +312,10 @@ public function choice_widget_collapsed(FormView $view, array $data)
}
if ($data['multiple']) {
$view->vars['attr']['multiple'] = 'multiple';
$data['attr']['multiple'] = 'multiple';
}
$string .= $this->renderer->block($view, 'widget_attributes', ['required' => $data['required']]) . '>';
$string .= $this->renderer->block($view, 'widget_attributes', $data) . '>';
if (null !== $data['placeholder']) {
$string .= '<option value=""';
if ( $data['required']
@@ -408,8 +407,9 @@ public function captcha_widget(FormView $view, array $data)
public function codemirror_widget(FormView $view, array $data)
{
//we set required to false, because codemirror doesn't play well with html5 validation..
$string = '<textarea ' . $this->renderer->block($view, 'widget_attributes', ['required' => false]) . '>';
// we set required to false, because codemirror doesn't play well with html5 validation..
$data['required'] = false;
$string = '<textarea ' . $this->renderer->block($view, 'widget_attributes', $data) . '>';
$string .= $data['value'] . '</textarea>';
if (!empty($data['codemirror_snippet'])) {
$snippet = str_replace('{$id}', $data['id'], $data['codemirror_snippet']);
@@ -479,12 +479,10 @@ public function image_widget(FormView $view, array $data)
$string .= $this->renderer->widget($data['form']['file']);
if (array_key_exists('title', $view->children)) {
$view->children['title']->vars['attr']['placeholder'] = $this->renderer->humanize('title');
$string .= $this->renderer->widget($view->children['title']);
$string .= $this->renderer->widget($view->children['title'], ['attr' => ['placeholder' => $this->renderer->humanize('title')]]);
}
if (array_key_exists('description', $view->children)) {
$view->children['description']->vars['attr']['placeholder'] = $this->renderer->humanize('description');
$string .= $this->renderer->widget($view->children['description']);
$string .= $this->renderer->widget($view->children['description'], ['attr' => ['placeholder' => $this->renderer->humanize('description')]]);
}
if (array_key_exists('score', $view->children)) {
$string .= $this->renderer->widget($view->children['score']);
@@ -497,20 +495,22 @@ public function image_widget(FormView $view, array $data)
public function subform_widget(FormView $view, array $data)
{
$view->vars['attr']['data-prototype'] = $this->escape($this->renderer->row($view->vars['prototype']));
$view->vars['attr']['data-max-count'] = $view->vars['max_count'];
$string = $this->renderer->widget($data['form'], $view->vars);
$data['attr']['data-prototype'] = $this->escape($this->renderer->row($view->vars['prototype']));
$data['attr']['data-max-count'] = $view->vars['max_count'];
$string = $this->renderer->widget($data['form'], $data);
return $string . $this->jsinit('init_subform("' . $view->vars['id'] . '", ' . $view->vars['sortable'] . ');');
}
public function submit_widget(FormView $view, array $data)
{
return $this->renderer->block($view, 'button_widget', ['type' => isset($data['type']) ? $data['type'] : 'submit']);
$data['type'] = isset($data['type']) ? $data['type'] : 'submit';
return $this->renderer->block($view, 'button_widget', $data);
}
public function delete_widget(FormView $view, array $data)
{
return $this->renderer->block($view, 'button_widget', ['type' => isset($data['type']) ? $data['type'] : 'delete']);
$data['type'] = isset($data['type']) ? $data['type'] : 'delete';
return $this->renderer->block($view, 'button_widget', $data);
}
public function textarea_widget(FormView $view, array $data)
@@ -520,25 +520,28 @@ public function textarea_widget(FormView $view, array $data)
$string = $this->get_view_renderer()->text_widget($view, $data);
return $string . $this->renderer->block($view, 'form_widget_simple', ['type' => "hidden"]);
}
$view->vars['attr']['class'] = 'longtext';
$view->vars['attr']['cols'] = 50;
return '<textarea ' . $this->renderer->block($view, 'widget_attributes') . '>' . $data['value'] . '</textarea>';
$data['attr']['class'] = 'longtext';
$data['attr']['cols'] = 50;
return '<textarea ' . $this->renderer->block($view, 'widget_attributes', $data) . '>' . $data['value'] . '</textarea>';
}
public function markdown_widget(FormView $view, array $data)
{
$string = '<textarea ' . $this->renderer->block($view, 'widget_attributes', ['required' => false]) . '>' . $data['value'] . '</textarea>';
$data['required'] = false;
$string = '<textarea ' . $this->renderer->block($view, 'widget_attributes', $data) . '>' . $data['value'] . '</textarea>';
return $string . $this->jsinit('var simplemde = new SimpleMDE({ element: document.getElementById("' . $view->vars['id'] . '"), status: false });');
}
public function tinymce_widget(FormView $view, array $data)
{
if ($view->vars['readonly']) {
$string = $this->get_view_renderer()->text_widget($view, $data);
return $string . $this->renderer->block($view, 'form_widget_simple', ['type' => "hidden"]);
$data['type'] = 'hidden';
return $string . $this->renderer->block($view, 'form_widget_simple', $data);
}
//we set required to false, because tinymce doesn't play well with html5 validation..
$string = '<textarea ' . $this->renderer->block($view, 'widget_attributes', ['required' => false]) . '>' . $data['value'] . '</textarea>';
// we set required to false, because tinymce doesn't play well with html5 validation..
$data['required'] = false;
$string = '<textarea ' . $this->renderer->block($view, 'widget_attributes', $data) . '>' . $data['value'] . '</textarea>';
return $string . $this->jsinit($data['tinymce_snippet']);
}
@@ -294,7 +294,7 @@ public function checkbox_widget(FormView $view, $data)
public function codemirror_widget(FormView $view, array $data)
{
$string = '<textarea ' . $this->renderer->block($view, 'widget_attributes') . '>';
$string = '<textarea ' . $this->renderer->block($view, 'widget_attributes', $data) . '>';
$string .= $data['value'] . '</textarea>';
if (!empty($data['codemirror_snippet'])) {
$snippet = str_replace('{$id}', $data['id'], $data['codemirror_snippet']);

0 comments on commit 3222b4a

Please sign in to comment.
You can’t perform that action at this time.