Skip to content

v5+Articles form

Adding new fields

To add/change the behaviour of the fields in the article form (the area above the article editor):

  • Create a form type extension by creating a class in src/App/Bundle/CmsAdminBundle/Form/Extension/PageArticleEditFormTypeExtension.php:

    php
    <?php
    
    namespace App\Bundle\CmsAdminBundle\Form\Extension;
    
    use Symfony\Component\Form\AbstractTypeExtension;
    use Symfony\Component\Form\FormBuilderInterface;
    
    class PageArticleEditFormTypeExtension extends AbstractTypeExtension
    {
        public function getExtendedType()
        {
            return \Wf\Bundle\CmsBaseAdminBundle\Form\Type\PageArticleEditFormType::class;
        }
    
        public function buildForm(FormBuilderInterface $builder, array $options)
        {
          // Add custom logic here
        }
    }
  • Change the form in the buildForm method.

For example, to add a new field

php
$builder->add('customField', TextType::class, array(
	'label' => 'Custom Field',
	'constraints' => array(
		new \Symfony\Component\Validator\Constraints\NotBlank(),
		new \Symfony\Component\Validator\Constraints\Length(25)
	)
));

NOTE that in the example above you must add use Symfony\Component\Form\Extension\Core\Type\TextType; in the head of the file if the IDE doesn't add it for you

To change an existing field, you must remove it first from the builder and then add the desired one:

php
$builder->remove('seoTitle');
$builder->add('seoTitle', $type, $options);
  • Edit the template and display it: src/Portafolio/Bundle/CmsAdminBundle/Resources/views/Form/articleEditForm.html.twig. Check the blocks defined in vendor/wfcms/cms-base-admin-bundle/Wf/Bundle/CmsBaseAdminBundle/Resources/views/Form/base_page_edit_form.html.twig. Each form input in this form is wrapped in a block. Overwrite the one where you want to display the custom field. For example, to add the input below the sections multi-select in the "Datos basicos" tab:
html
{% block wf_pef_m_sections %}
	{{ parent() }}
	{{ form_row(pageEditForm.commentsSetting, {attr: {
		'data-bind': 'value:customField',
		'data-form-field': 'customField'
		}}) }}
{% endblock %}

For radio inputs or expanded checkboxes (checkboxes with multiple values, not a "yes/no" single checkbox), the attributes are passed in a different manner:

html
{% block wf_pef_m_sections %}
	{{ parent() }}
	{{ form_row(pageEditForm.expandedChoiceSetting, {widgetAttr: {
		'data-bind': 'checked:customField',
		'data-form-field': 'customField'
		}}) }}
{% endblock %}
  • Add the getter/setter for this field to the PageArticle object. You can either add a custom doctrine column to hold the data, but for most cases, using the settings array is a better choice, as it doesn't require a database migration:
php
public function getCustomField()
{
    return $this->getSettings('customField', __DEFAULT_VALUE__);
}

public function setCustomField($customField)
{
    $this->addSetting('customField', $customField);
}

Adding a field only to certain templates

To add a field to a certain template, follow the steps above, but adding the field to the builder should be done in a listener:

php
$builder->addEventListener(FormEvents::PRE_SET_DATA, function (FormEvent $event) use ($builder) {
	$form = $event->getForm();
	$data = $event->getData();

	if ($data instanceof Page) {
	    switch ($data->getTemplate()) {
	    	case 'template1':
		    	$form->add($builder->add('customField', $type, $options)->getForm());
	    		break;
	    }
    }
});