v3-4Articles form
Adding new fields
To add/change the behaviour of the fields in the article form (the area above the article editor):
- Overwrite the form type's class by defining the
wf_cms_admin.form.type.page_article_edit.class
(app/config/parameters/project.yml) parameter if it isn't already defined. Set it to point to a class in the project:__PROJECT__\Bundle\CmsAdminBundle\Form\Type\PageArticleEditFormType
- Create this class and make it extend
Wf\Bundle\CmsBaseAdminBundle\Form\Type\CategoryBoundArticleEditFormType
- Overwrite the method that builds the form. The base class calls
buildFormType
,buildBaseFields
(fields displayed in the "Datos basicos" tab),buildMetadataForm
("Metadatos" + "SEO" tabs) - In this method you can use
$this->builder
object to add new fields to the form.
For example, to add a new field
php
$this->builder->add('customField', 'text', array(
'label' => 'Custom Field',
'constraints' => array(
new \Symfony\Component\Validator\Constraints\NotBlank(),
new \Symfony\Component\Validator\Constraints\Length(25)
)
));
To change an existing field, you must remove it first from the builder and then add the desired one:
php
$this->builder->remove('seoTitle');
$this->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 invendor/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 thesettings
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 = $this->builder;
$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;
}
}
});