3. Usage

3.1. Creating new content lists

Here is an example on how to create a new content list:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
// src/AppBundle/Controller/MyController.php

use SWP\Component\ContentList\Model\ContentListInterface;
// ...
public function createAction()
{
    $repository = $this->container->get('swp.repository.content_list');

    /* @var ContentListInterface $contentList */
    $contentList = $this->get('swp.factory.content_list')->create();
    $contentList->setName('my content list');
    $contentList->setDescription('description');
    $contentList->setLimit(10);
    $contentList->setType(ContentListInterface::TYPE_AUTOMATIC);
    // ...

    $repository->add($contentList);

    // ...
}

3.2. Adding new items to content list

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
// src/AppBundle/Controller/MyController.php

use SWP\Component\ContentList\Model\ContentListInterface;
use SWP\Component\ContentList\Model\ContentListItemInterface;
use Acme\AppBundle\Entity\Article;
// ...
public function createAction()
{
    $repository = $this->container->get('swp.repository.content_list');

    /* @var ContentListInterface $contentList */
    $contentList = $this->get('swp.factory.content_list')->create();
    $contentList->setName('my content list');
    $contentList->setDescription('description');
    $contentList->setLimit(10);
    $contentList->setType(ContentListInterface::TYPE_AUTOMATIC);
    // ...

    /* @var ContentListItemInterface $contentListItem */
    $contentListItem = $this->get('swp.factory.content_list_item')->create();
    $contentListItem->setPosition(6);
    $contentListItem->setContent(new Article());
    $contentListItem->setSticky(true);
    $contentList->addItem($contentListItem);

    $repository->add($contentList);

    // ...
}

Note

Article class must implement SWP\Component\ContentList\Model\ListContentInterface.

3.3. Deleting content lists

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
// src/AppBundle/Controller/MyController.php

use SWP\Component\ContentList\Model\ContentListInterface;
use Acme\AppBundle\Entity\Article;
// ...
public function deleteAction($id)
{
    $repository = $this->container->get('swp.repository.content_list');

    /* @var ContentListInterface $contentList */
    $contentList = $repository->findOneBy(['id' => $id]);
    // ...

    $repository->remove($contentList);

    // ...
}

3.4. Deleting content lists items

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
// src/AppBundle/Controller/MyController.php

use SWP\Component\ContentList\Model\ContentListItemInterface;
use Acme\AppBundle\Entity\Article;
// ...
public function deleteAction($id)
{
    $repository = $this->container->get('swp.repository.content_list_item');

    /* @var ContentListItemInterface $contentListItem */
    $contentListItem = $repository->findOneBy(['id' => $id]);
    // ...

    $repository->remove($contentListItem);

    // ...
}

3.5. Forms

3.5.1. Content list type selector

If you want to use content list type selector inside your custom form you can do it by adding SWP\Bundle\ContentListBundle\Form\Type\ContentListTypeSelectorType form field type to your form:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
namespace Acme\AppBundle\Form\Type;

use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\Extension\Core\Type\TextType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\Validator\Constraints\NotBlank;


class MyListType extends AbstractType
{
    public function buildForm(FormBuilderInterface $builder, array $options)
    {
        $builder->add('name', TextType::class, [
            'constraints' => [
                new NotBlank(),
            ],
            'description' => 'List name',
        ])
        ->add('type', ContentListTypeSelectorType::class, [
            'constraints' => [
                new NotBlank(),
            ],
            'description' => 'List type',
        ])
    }
}

Alternatively, you could also extend from the default SWP\Bundle\ContentListBundle\Form\Type\ContentListType class if you would only add more fields on top of the existing form.

Note

For more details on how to register custom factory, repository, object manager, forms using custom classes see SWPStorageBundle Usage section.

3.6. Getting content lists from repository

To get single or all content lists from the repository you can use default Doctrine ORM SWP\Bundle\ContentListBundle\Doctrine\ORM\ContentListRepository repository. It has the same methods as Doctrine ORM EntityRepository, but it contains an extra method to get content lists by its type:

  • findByType(string $type): array - it gets many content lists by its type, type can be either: automatic or manual.
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
// src/AppBundle/Controller/MyController.php

use SWP\Component\ContentList\Model\ContentListInterface;
// ...
public function getAction()
{
    $repository = $this->container->get('swp.repository.content_list');

    $lists = $repository->findByType(ContentListInterface::TYPE_AUTOMATIC);
    var_dump($lists);die;
    // ...
}

Note

This repository is automatically registered as a service for you and can be accessible under service id: swp.repository.content_list in Symfony container.

3.7. Getting content lists items from repository

To get content list items you can use default repository which is registered as a service under the swp.repository.content_list_item key in Symfony container. It extends default Doctrine ORM EntityRepository.