/** * Generates a FormView element for this field, copying all the * data over to the view in a view-friendly format * @return \snb\form\FormView */ public function getView() { // Create the view $view = new FormView(); // Copy over any properties into the view foreach ($this->properties as $name => $value) { $view->set($name, $value); } // Custom values that are generated or outside the standard properties $view->set('id', $this->getId()); $view->set('type', $this->getType()); $view->set('full_name', $this->getFullName()); $view->set('errors', $this->errors); return $view; }
/** * Calls one of the blocks in the template. It tries using a block * called <type>_<action>, and if that does not exist, falls back to field_<action> * @param string $action * @param \snb\form\FormView $data * @return string */ public function render($action, $data) { // make sure we got something if ($data == null) { return ''; } // In fact, it must be a FormView... if (!$data instanceof FormView) { return ''; } // Get to work, finding a suitable block to render the Form view with $blocks = $this->template->getBlocks(); $type = $data->get('type', 'field'); $typeAction = $type . '_' . $action; if (!array_key_exists($typeAction, $blocks)) { $typeAction = 'field_' . $action; } ob_start(); $this->template->displayBlock($typeAction, $data->all(), $blocks); $html = ob_get_clean(); return $html; }
/** * Build the view, which may consist of child elements * @return \snb\form\FormView */ public function getView() { // Build the view $view = parent::getView(); // If this set of choices is meant to be expanded, // then generate the child controls. $multiselect = $this->get('multiselect', false); if ($this->get('expanded', false)) { // yes, this is an expanded control $choices = $this->get('choices', array()); // Get the value - make sure it is an array, as that simplifies the code below $value = $this->get('value'); if (!is_array($value)) { $value = array($value); } $checkedItemCount = 0; $firstChild = null; foreach ($choices as $key => $title) { // make a checkbox for each item please. $child = new FormView(); if ($firstChild == null) { $firstChild = $child; } // We only copy over / set up a limited set of properties // eg. if we copied "hint", every expanded item would have // the same hint next to it $child->set('id', $this->getId() . '-' . $key); $child->set('label', $title); $child->set('value', $key); $child->set('name', $key); // Decide if we want checkboxes or radio buttons if ($multiselect) { $child->set('type', 'checkbox'); $child->set('full_name', $this->getFullName() . '[' . $key . ']'); } else { $child->set('type', 'radio'); $child->set('full_name', $this->getFullName()); } // handle attributes $attr = $this->get('attributes', array()); if ($this->get('readonly')) { $attr['disabled'] = 'disabled'; } // See if this item is checked or not if (in_array($key, $value)) { $attr['checked'] = 'checked'; $checkedItemCount++; } // Set any attributes on the control that we have been adjusting $child->set('attributes', $attr); // add it $view->addChild($child); } // If we created radio buttons, and none of them were checked, // then check the first one if (!$multiselect && $checkedItemCount == 0 && $firstChild != null) { $attr = $firstChild->get('attributes', array()); $attr['checked'] = 'checked'; $firstChild->set('attributes', $attr); } } return $view; }