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

Commit

Permalink
Merge pull request #22 from qsomazzi/improve_fragments
Browse files Browse the repository at this point in the history
Add fragment backoffice title and improve validation
  • Loading branch information
rande committed Dec 2, 2016
2 parents b038ad5 + 2f94ba6 commit 18daa99
Show file tree
Hide file tree
Showing 15 changed files with 178 additions and 51 deletions.
5 changes: 5 additions & 0 deletions Admin/ArticleAdmin.php
Expand Up @@ -108,6 +108,11 @@ public function validate(ErrorElement $errorElement, $object)
AbstractArticle::getStatuses() : AbstractArticle::getContributorStatus()))
->end()
;

$fragmentAdmin = $this->getChild('sonata.article.admin.fragment');
foreach ($object->getFragments() as $fragment) {
$fragmentAdmin->validate($errorElement, $fragment);
}
}

/**
Expand Down
19 changes: 17 additions & 2 deletions FragmentService/AbstractFragmentService.php
Expand Up @@ -13,8 +13,8 @@

use Sonata\AdminBundle\Form\FormMapper;
use Sonata\ArticleBundle\Model\FragmentInterface;
use Sonata\CoreBundle\Validator\ErrorElement;
use Symfony\Component\OptionsResolver\OptionsResolver;
use Symfony\Component\Validator\Context\ExecutionContextInterface;

/**
* @author Hugo Briand <briand@ekino.com>
Expand Down Expand Up @@ -54,6 +54,16 @@ final public function buildCreateForm(FormMapper $form, FragmentInterface $fragm
* {@inheritdoc}
*/
public function buildEditForm(FormMapper $form, FragmentInterface $fragment)
{
// Add BO title
$form->add('backofficeTitle');
$this->buildForm($form, $fragment);
}

/**
* {@inheritdoc}
*/
public function buildForm(FormMapper $form, FragmentInterface $fragment)
{
}

Expand Down Expand Up @@ -109,8 +119,13 @@ public function configureOptions(OptionsResolver $resolver)
/**
* {@inheritdoc}
*/
public function validate(FragmentInterface $fragment, ExecutionContextInterface $context)
public function validate(ErrorElement $errorElement, $object)
{
if (empty($object->getBackofficeTitle())) {
$errorElement
->addViolation(sprintf('Fragment %s - `Backoffice Title` must not be empty', $this->getName()))
;
}
}

/**
Expand Down
18 changes: 13 additions & 5 deletions FragmentService/FragmentServiceInterface.php
Expand Up @@ -13,8 +13,8 @@

use Sonata\AdminBundle\Form\FormMapper;
use Sonata\ArticleBundle\Model\FragmentInterface;
use Sonata\CoreBundle\Validator\ErrorElement;
use Symfony\Component\OptionsResolver\OptionsResolver;
use Symfony\Component\Validator\Context\ExecutionContextInterface;

/**
* @author Hugo Briand <briand@ekino.com>
Expand All @@ -38,12 +38,20 @@ public function buildEditForm(FormMapper $form, FragmentInterface $fragment);
public function buildCreateForm(FormMapper $form, FragmentInterface $fragment);

/**
* Validates the fragment (you'll need to add your violations through context).
* Builds the common part of creation|edition form for the fragment.
*
* @param FragmentInterface $fragment
* @param ExecutionContextInterface $context
* @param FormMapper $form
* @param FragmentInterface $fragment
*/
public function buildForm(FormMapper $form, FragmentInterface $fragment);

/**
* Validates the fragment (you'll need to add your violations through $errorElement).
*
* @param ErrorElement $errorElement
* @param object $object
*/
public function validate(FragmentInterface $fragment, ExecutionContextInterface $context);
public function validate(ErrorElement $errorElement, $object);

/**
* Returns the Fragment service readable name.
Expand Down
14 changes: 6 additions & 8 deletions FragmentService/TextFragmentService.php
Expand Up @@ -13,8 +13,8 @@

use Sonata\AdminBundle\Form\FormMapper;
use Sonata\ArticleBundle\Model\FragmentInterface;
use Sonata\CoreBundle\Validator\ErrorElement;
use Symfony\Component\Validator\Constraints\NotBlank;
use Symfony\Component\Validator\Context\ExecutionContextInterface;

/**
* @author Hugo Briand <briand@ekino.com>
Expand All @@ -24,7 +24,7 @@ class TextFragmentService extends AbstractFragmentService
/**
* {@inheritdoc}
*/
public function buildEditForm(FormMapper $form, FragmentInterface $fragment)
public function buildForm(FormMapper $form, FragmentInterface $fragment)
{
$form->add('settings', 'sonata_type_immutable_array', array(
'keys' => array(
Expand All @@ -42,13 +42,11 @@ public function buildEditForm(FormMapper $form, FragmentInterface $fragment)
/**
* {@inheritdoc}
*/
public function validate(FragmentInterface $fragment, ExecutionContextInterface $context)
public function validate(ErrorElement $errorElement, $object)
{
if (empty($fragment->getSettings()['text'])) {
$context
->buildViolation('`Text` must not be empty')
->atPath('settings.text')
->addViolation()
if (empty($object->getSetting('text'))) {
$errorElement
->addViolation('Fragment Text - `Text` must not be empty')
;
}
}
Expand Down
23 changes: 10 additions & 13 deletions FragmentService/TitleFragmentService.php
Expand Up @@ -13,9 +13,9 @@

use Sonata\AdminBundle\Form\FormMapper;
use Sonata\ArticleBundle\Model\FragmentInterface;
use Sonata\CoreBundle\Validator\ErrorElement;
use Symfony\Component\Validator\Constraints\Length;
use Symfony\Component\Validator\Constraints\NotBlank;
use Symfony\Component\Validator\Context\ExecutionContextInterface;

/**
* @author Hugo Briand <briand@ekino.com>
Expand All @@ -25,7 +25,7 @@ class TitleFragmentService extends AbstractFragmentService
/**
* {@inheritdoc}
*/
public function buildEditForm(FormMapper $form, FragmentInterface $fragment)
public function buildForm(FormMapper $form, FragmentInterface $fragment)
{
$form->add('settings', 'sonata_type_immutable_array', array(
'keys' => array(
Expand All @@ -44,20 +44,17 @@ public function buildEditForm(FormMapper $form, FragmentInterface $fragment)
/**
* {@inheritdoc}
*/
public function validate(FragmentInterface $fragment, ExecutionContextInterface $context)
public function validate(ErrorElement $errorElement, $object)
{
if (empty($fragment->getSettings()['text'])) {
$context
->buildViolation('`Title` must not be empty')
->atPath('settings.text')
->addViolation()
if (empty($object->getSetting('text'))) {
$errorElement
->addViolation('Fragment Title - `Text` must not be empty')
;
}
if (strlen($fragment->getSetting('text')) > 255) {
$context
->buildViolation('`Title` must not be longer than 255 characters.')
->atPath('settings.text')
->addViolation()

if (strlen($object->getSetting('text')) > 255) {
$errorElement
->addViolation('Fragment Text - `Text` must not be longer than 255 characters.')
;
}
}
Expand Down
31 changes: 28 additions & 3 deletions Model/AbstractFragment.php
Expand Up @@ -49,18 +49,23 @@ abstract class AbstractFragment implements FragmentInterface, ArticleFragmentInt
protected $position;

/**
* @var int
* @var string
*/
protected $backofficeTitle;

/**
* @var ArticleInterface
*/
protected $article;

/**
* Returns type of fragment.
* Returns Backoffice title of fragment.
*
* @return string
*/
public function __toString()
{
return (string) $this->getType();
return $this->getBackofficeTitle();
}

/**
Expand Down Expand Up @@ -218,4 +223,24 @@ public function getUpdatedAt()
{
return $this->updatedAt;
}

/**
* @return string
*/
public function getBackofficeTitle()
{
return $this->backofficeTitle;
}

/**
* @param string $backofficeTitle
*
* @return $this
*/
public function setBackofficeTitle($backofficeTitle)
{
$this->backofficeTitle = $backofficeTitle;

return $this;
}
}
1 change: 1 addition & 0 deletions Resources/config/doctrine/AbstractFragment.orm.xml
@@ -1,6 +1,7 @@
<?xml version="1.0" encoding="UTF-8"?>
<doctrine-mapping xmlns="http://doctrine-project.org/schemas/orm/doctrine-mapping" xsi="http://www.w3.org/2001/XMLSchema-instance" schemaLocation="http://doctrine-project.org/schemas/orm/doctrine-mapping http://doctrine-project.org/schemas/orm/doctrine-mapping.xsd">
<mapped-superclass name="Sonata\ArticleBundle\Entity\AbstractFragment">
<field name="backofficeTitle" type="string" column="backoffice_title" length="255"/>
<field name="position" type="integer" column="position" nullable="true"/>
<field name="settings" type="json" column="settings"/>
<field name="type" type="string" column="type" length="64"/>
Expand Down
1 change: 1 addition & 0 deletions Resources/doc/index.rst
Expand Up @@ -14,3 +14,4 @@ Reference Guide
reference/getting_started
reference/configuration
reference/custom_fragment
reference/todo
11 changes: 11 additions & 0 deletions Resources/doc/reference/todo.rst
@@ -0,0 +1,11 @@
Todo
====

This bundle is still a work in progress, we need to work more on some parts.
We will try to keep this "Todo list" up to date to inform you of our expectations.

- Find a better way to handle fragment validation
- Add articleTranslation feature (each articles are translatable and related to a Site)
- Publishing Workflow
- Handle formats (web, json, ...)
- Duplicate article
4 changes: 3 additions & 1 deletion Resources/public/js/editOneAssociationFragment.js
Expand Up @@ -5,6 +5,7 @@ jQuery(document).ready(function() {

var id = $selector.data('id');
var fragmentAddUrl = $selector.data('add-fragment-url');
var translations = $selector.data('translations');

/**
* Get URL value to POST a new fragment
Expand Down Expand Up @@ -34,6 +35,7 @@ jQuery(document).ready(function() {
formSource: '#field_widget_'+id,
addButtonSource: '#field_actions_'+id,
getAddUrl: getFragmentAddUrl,
addedCallback: addFragmentCallback
addedCallback: addFragmentCallback,
translations: translations
});
});

0 comments on commit 18daa99

Please sign in to comment.