Skip to content
This repository has been archived by the owner on Jul 10, 2020. It is now read-only.

Provide a way of ignore custom view helpers, plus update coding style #108

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 23 additions & 4 deletions config/module.config.php
Original file line number Diff line number Diff line change
@@ -1,8 +1,25 @@
<?php

return array(
'view_helpers' => array(
'invokables' => array(
'twbbundle' => array (
'ignoredViewHelpers' => array (
'file',
'checkbox',
'radio',
'submit',
'multi_checkbox',
'static',
'button',
'reset'
)
),
'service_manager' => array (
'factories' => array (
'TwbBundle\Options\ModuleOptions' => 'TwbBundle\Options\Factory\ModuleOptionsFactory'
)
),
'view_helpers' => array (
'invokables' => array (
//Alert
'alert' => 'TwbBundle\View\Helper\TwbBundleAlert',
//Badge
Expand All @@ -17,7 +34,6 @@
'formSubmit' => 'TwbBundle\Form\View\Helper\TwbBundleFormButton',
'formCheckbox' => 'TwbBundle\Form\View\Helper\TwbBundleFormCheckbox',
'formCollection' => 'TwbBundle\Form\View\Helper\TwbBundleFormCollection',
'formElement' => 'TwbBundle\Form\View\Helper\TwbBundleFormElement',
'formElementErrors' => 'TwbBundle\Form\View\Helper\TwbBundleFormElementErrors',
'formMultiCheckbox' => 'TwbBundle\Form\View\Helper\TwbBundleFormMultiCheckbox',
'formRadio' => 'TwbBundle\Form\View\Helper\TwbBundleFormRadio',
Expand All @@ -31,6 +47,9 @@
'fontAwesome' => 'TwbBundle\View\Helper\TwbBundleFontAwesome',
//Label
'label' => 'TwbBundle\View\Helper\TwbBundleLabel'
),
'factories' => array (
'formElement' => 'TwbBundle\Form\View\Helper\Factory\TwbBundleFormElementFactory',
)
)
),
);
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<?php

namespace TwbBundle\Form\View\Helper\Factory;

use Zend\ServiceManager\FactoryInterface;
use Zend\ServiceManager\ServiceLocatorInterface;
use TwbBundle\Form\View\Helper\TwbBundleFormElement;

/**
* Factory to inject the ModuleOptions hard dependency
*
* @author Fábio Carneiro <fahecs@gmail.com>
* @license MIT
*/
class TwbBundleFormElementFactory implements FactoryInterface
{
public function createService(ServiceLocatorInterface $serviceLocator)
{
$options = $serviceLocator->getServiceLocator()->get('TwbBundle\Options\ModuleOptions');
return new TwbBundleFormElement($options);
}
}
73 changes: 38 additions & 35 deletions src/TwbBundle/Form/View/Helper/TwbBundleForm.php
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
<?php
namespace TwbBundle\Form\View\Helper;

class TwbBundleForm extends \Zend\Form\View\Helper\Form
use Zend\Form\View\Helper\Form;
use Zend\Form\FormInterface;
use Zend\Form\FieldsetInterface;

class TwbBundleForm extends Form
{
const LAYOUT_HORIZONTAL = 'horizontal';
const LAYOUT_INLINE = 'inline';
Expand All @@ -19,79 +23,78 @@ class TwbBundleForm extends \Zend\Form\View\Helper\Form
protected $formLayout = null;

/**
* @see \Zend\Form\View\Helper\Form::__invoke()
* @param \Zend\Form\FormInterface $oForm
* @see Form::__invoke()
* @param FormInterface $oForm
* @param string $sFormLayout
* @return \TwbBundle\Form\View\Helper\TwbBundleForm|string
* @return TwbBundleForm|string
*/
public function __invoke(\Zend\Form\FormInterface $oForm = null, $sFormLayout = self::LAYOUT_HORIZONTAL)
public function __invoke(FormInterface $oForm = null, $sFormLayout = self::LAYOUT_HORIZONTAL)
{
if ($oForm) {
return $this->render($oForm, $sFormLayout);
}
$this->formLayout = $sFormLayout;
return $this;
}
}

/**
* Render a form from the provided $oForm,
* @see \Zend\Form\View\Helper\Form::render()
* @param \Zend\Form\FormInterface $oForm
* @see Form::render()
* @param FormInterface $oForm
* @param string $sFormLayout
* @return string
*/
public function render(\Zend\Form\FormInterface $oForm, $sFormLayout = self::LAYOUT_HORIZONTAL)
public function render(FormInterface $oForm, $sFormLayout = self::LAYOUT_HORIZONTAL)
{
//Prepare form if needed
if (method_exists($oForm, 'prepare')) {
$oForm->prepare();
}
//Prepare form if needed
if (method_exists($oForm, 'prepare')) {
$oForm->prepare();
}

$this->setFormClass($oForm, $sFormLayout);
$this->setFormClass($oForm, $sFormLayout);

//Set form role
if (!$oForm->getAttribute('role')) {
//Set form role
if (!$oForm->getAttribute('role')) {
$oForm->setAttribute('role', 'form');
}

$bHasColumnSizes = false;
$sFormContent = '';
$oRenderer = $this->getView();
foreach($oForm as $oElement){
$aOptions = $oElement->getOptions();
if (!$bHasColumnSizes && !empty($aOptions['column-size'])) {
$sFormContent = '';
$oRenderer = $this->getView();
foreach ($oForm as $oElement) {
$aOptions = $oElement->getOptions();
if (!$bHasColumnSizes && !empty($aOptions['column-size'])) {
$bHasColumnSizes = true;
}
//Define layout option to form elements if not already defined
if($sFormLayout && empty($aOptions['twb-layout'])){
if ($sFormLayout && empty($aOptions['twb-layout'])) {
$aOptions['twb-layout'] = $sFormLayout;
$oElement->setOptions($aOptions);
}
$sFormContent .= $oElement instanceof \Zend\Form\FieldsetInterface?$oRenderer->formCollection($oElement):$oRenderer->formRow($oElement);
}
if ($bHasColumnSizes && $sFormLayout !== self::LAYOUT_HORIZONTAL) {
$oElement->setOptions($aOptions);
}
$sFormContent .= $oElement instanceof FieldsetInterface ? $oRenderer->formCollection($oElement) : $oRenderer->formRow($oElement);
}
if ($bHasColumnSizes && $sFormLayout !== self::LAYOUT_HORIZONTAL) {
$sFormContent = sprintf(self::$formRowFormat, $sFormContent);
}
return $this->openTag($oForm).$sFormContent.$this->closeTag();
return $this->openTag($oForm) . $sFormContent . $this->closeTag();
}

/**
* Sets form layout class
*
* @param \Zend\Form\FormInterface $oForm
* @param FormInterface $oForm
* @param string $sFormLayout
* @return void
*/
protected function setFormClass(\Zend\Form\FormInterface $oForm, $sFormLayout = self::LAYOUT_HORIZONTAL)
protected function setFormClass(FormInterface $oForm, $sFormLayout = self::LAYOUT_HORIZONTAL)
{
if(is_string($sFormLayout)){
if (is_string($sFormLayout)) {
$sLayoutClass = 'form-'.$sFormLayout;
if ($sFormClass = $oForm->getAttribute('class')) {
if (!preg_match('/(\s|^)' . preg_quote($sLayoutClass, '/') . '(\s|$)/', $sFormClass)) {
$oForm->setAttribute('class', trim($sFormClass . ' ' . $sLayoutClass));
}
}
else {
} else {
$oForm->setAttribute('class', $sLayoutClass);
}
}
Expand All @@ -100,10 +103,10 @@ protected function setFormClass(\Zend\Form\FormInterface $oForm, $sFormLayout =
/**
* Generate an opening form tag
*
* @param null|\Zend\Form\FormInterface $form
* @param null|FormInterface $form
* @return string
*/
public function openTag(\Zend\Form\FormInterface $form = null)
public function openTag(FormInterface $form = null)
{
$this->setFormClass($form, $this->formLayout);
return parent::openTag($form);
Expand Down
Loading