Skip to content

Commit

Permalink
render attributes based on data instead of view->vars so as to allow …
Browse files Browse the repository at this point in the history
…overriding during rendering
  • Loading branch information
flack committed Mar 25, 2019
1 parent f8c81e7 commit 3222b4a
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 39 deletions.
4 changes: 2 additions & 2 deletions src/midcom/datamanager/template/base.php
Expand Up @@ -63,10 +63,10 @@ public function form_rest(FormView $view, array $data)


public function widget_attributes(FormView $view, array $data) public function widget_attributes(FormView $view, array $data)
{ {
$attributes = $view->vars['attr']; $attributes = $data['attr'];
$attributes['id'] = $data['id']; $attributes['id'] = $data['id'];
$attributes['name'] = $data['full_name']; $attributes['name'] = $data['full_name'];
if (!empty($view->vars['readonly'])) { if (!empty($data['readonly'])) {
$attributes['readonly'] = 'readonly'; $attributes['readonly'] = 'readonly';
} }
if ($data['disabled']) { if ($data['disabled']) {
Expand Down
2 changes: 1 addition & 1 deletion src/midcom/datamanager/template/csv.php
Expand Up @@ -72,7 +72,7 @@ public function autocomplete_widget(FormView $view, array $data)
public function choice_widget_collapsed(FormView $view, array $data) public function choice_widget_collapsed(FormView $view, array $data)
{ {
if (isset($data['data'])) { if (isset($data['data'])) {
if (!empty($view->vars['multiple'])) { if (!empty($data['multiple'])) {
$selection = $data['data']; $selection = $data['data'];
} else { } else {
$selection = (string) $data['data']; $selection = (string) $data['data'];
Expand Down
73 changes: 38 additions & 35 deletions src/midcom/datamanager/template/form.php
Expand Up @@ -203,21 +203,20 @@ public function form_widget_simple(FormView $view, array $data)
{ {
$type = isset($data['type']) ? $data['type'] : 'text'; $type = isset($data['type']) ? $data['type'] : 'text';
if ($view->vars['readonly'] && $type !== 'hidden') { 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'])) { if (empty($data['attr']['class']) && in_array($type, ['text', 'password', 'email', 'url'])) {
$view->vars['attr']['class'] = $data['attr']['class']; $data['attr']['class'] = 'shorttext';
} elseif (in_array($type, ['text', 'password', 'email', 'url'])) {
$view->vars['attr']['class'] = 'shorttext';
} }


if ( !empty($data['value']) if ( !empty($data['value'])
|| is_numeric($data['value'])) { || is_numeric($data['value'])) {
$view->vars['attr']['value'] = $data['value']; $data['attr']['value'] = $data['value'];
} }
$view->vars['attr']['type'] = $type; $data['attr']['type'] = $type;
return '<input ' . $this->renderer->block($view, 'widget_attributes') . ' />'; return '<input ' . $this->renderer->block($view, 'widget_attributes', $data) . ' />';
} }


public function button_widget(FormView $view, array $data) public function button_widget(FormView $view, array $data)
Expand Down Expand Up @@ -263,25 +262,25 @@ public function autocomplete_widget(FormView $view, array $data)
return $string . $this->jsinit($jsinit); 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'])) { if (isset($data['value'])) {
$view->vars['attr']['value'] = $data['value']; $data['attr']['value'] = $data['value'];
} }
if ($data['checked']) { if ($data['checked']) {
$view->vars['attr']['checked'] = 'checked'; $data['attr']['checked'] = 'checked';
} }
} }


public function radio_widget(FormView $view, array $data) public function radio_widget(FormView $view, array $data)
{ {
$this->prepare_widget_attributes('radio', $view, $data); $this->prepare_widget_attributes('radio', $view, $data);
if ($view->vars['readonly']) { 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) public function checkbox_widget(FormView $view, array $data)
Expand All @@ -295,7 +294,7 @@ public function checkbox_widget(FormView $view, array $data)
} }
$this->prepare_widget_attributes('checkbox', $view, $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) public function choice_widget_collapsed(FormView $view, array $data)
Expand All @@ -313,10 +312,10 @@ public function choice_widget_collapsed(FormView $view, array $data)
} }


if ($data['multiple']) { 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']) { if (null !== $data['placeholder']) {
$string .= '<option value=""'; $string .= '<option value=""';
if ( $data['required'] if ( $data['required']
Expand Down Expand Up @@ -408,8 +407,9 @@ public function captcha_widget(FormView $view, array $data)


public function codemirror_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.. // we set required to false, because codemirror doesn't play well with html5 validation..
$string = '<textarea ' . $this->renderer->block($view, 'widget_attributes', ['required' => false]) . '>'; $data['required'] = false;
$string = '<textarea ' . $this->renderer->block($view, 'widget_attributes', $data) . '>';
$string .= $data['value'] . '</textarea>'; $string .= $data['value'] . '</textarea>';
if (!empty($data['codemirror_snippet'])) { if (!empty($data['codemirror_snippet'])) {
$snippet = str_replace('{$id}', $data['id'], $data['codemirror_snippet']); $snippet = str_replace('{$id}', $data['id'], $data['codemirror_snippet']);
Expand Down Expand Up @@ -479,12 +479,10 @@ public function image_widget(FormView $view, array $data)
$string .= $this->renderer->widget($data['form']['file']); $string .= $this->renderer->widget($data['form']['file']);


if (array_key_exists('title', $view->children)) { if (array_key_exists('title', $view->children)) {
$view->children['title']->vars['attr']['placeholder'] = $this->renderer->humanize('title'); $string .= $this->renderer->widget($view->children['title'], ['attr' => ['placeholder' => $this->renderer->humanize('title')]]);
$string .= $this->renderer->widget($view->children['title']);
} }
if (array_key_exists('description', $view->children)) { if (array_key_exists('description', $view->children)) {
$view->children['description']->vars['attr']['placeholder'] = $this->renderer->humanize('description'); $string .= $this->renderer->widget($view->children['description'], ['attr' => ['placeholder' => $this->renderer->humanize('description')]]);
$string .= $this->renderer->widget($view->children['description']);
} }
if (array_key_exists('score', $view->children)) { if (array_key_exists('score', $view->children)) {
$string .= $this->renderer->widget($view->children['score']); $string .= $this->renderer->widget($view->children['score']);
Expand All @@ -497,20 +495,22 @@ public function image_widget(FormView $view, array $data)


public function subform_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'])); $data['attr']['data-prototype'] = $this->escape($this->renderer->row($view->vars['prototype']));
$view->vars['attr']['data-max-count'] = $view->vars['max_count']; $data['attr']['data-max-count'] = $view->vars['max_count'];
$string = $this->renderer->widget($data['form'], $view->vars); $string = $this->renderer->widget($data['form'], $data);
return $string . $this->jsinit('init_subform("' . $view->vars['id'] . '", ' . $view->vars['sortable'] . ');'); return $string . $this->jsinit('init_subform("' . $view->vars['id'] . '", ' . $view->vars['sortable'] . ');');
} }


public function submit_widget(FormView $view, array $data) 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) 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) public function textarea_widget(FormView $view, array $data)
Expand All @@ -520,25 +520,28 @@ public function textarea_widget(FormView $view, array $data)
$string = $this->get_view_renderer()->text_widget($view, $data); $string = $this->get_view_renderer()->text_widget($view, $data);
return $string . $this->renderer->block($view, 'form_widget_simple', ['type' => "hidden"]); return $string . $this->renderer->block($view, 'form_widget_simple', ['type' => "hidden"]);
} }
$view->vars['attr']['class'] = 'longtext'; $data['attr']['class'] = 'longtext';
$view->vars['attr']['cols'] = 50; $data['attr']['cols'] = 50;
return '<textarea ' . $this->renderer->block($view, 'widget_attributes') . '>' . $data['value'] . '</textarea>'; return '<textarea ' . $this->renderer->block($view, 'widget_attributes', $data) . '>' . $data['value'] . '</textarea>';
} }


public function markdown_widget(FormView $view, array $data) 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 });'); return $string . $this->jsinit('var simplemde = new SimpleMDE({ element: document.getElementById("' . $view->vars['id'] . '"), status: false });');
} }


public function tinymce_widget(FormView $view, array $data) public function tinymce_widget(FormView $view, array $data)
{ {
if ($view->vars['readonly']) { if ($view->vars['readonly']) {
$string = $this->get_view_renderer()->text_widget($view, $data); $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.. // 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>'; $data['required'] = false;
$string = '<textarea ' . $this->renderer->block($view, 'widget_attributes', $data) . '>' . $data['value'] . '</textarea>';
return $string . $this->jsinit($data['tinymce_snippet']); return $string . $this->jsinit($data['tinymce_snippet']);
} }


Expand Down
2 changes: 1 addition & 1 deletion src/midcom/datamanager/template/view.php
Expand Up @@ -294,7 +294,7 @@ public function checkbox_widget(FormView $view, $data)


public function codemirror_widget(FormView $view, array $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>'; $string .= $data['value'] . '</textarea>';
if (!empty($data['codemirror_snippet'])) { if (!empty($data['codemirror_snippet'])) {
$snippet = str_replace('{$id}', $data['id'], $data['codemirror_snippet']); $snippet = str_replace('{$id}', $data['id'], $data['codemirror_snippet']);
Expand Down

0 comments on commit 3222b4a

Please sign in to comment.