2017 © Pedro Peláez
 

sylius-plugin sylius-brand-plugin

A Sylius plugin for handling brands

image

loevgaard/sylius-brand-plugin

A Sylius plugin for handling brands

  • Friday, July 13, 2018
  • by loevgaard
  • Repository
  • 1 Watchers
  • 1 Stars
  • 13 Installations
  • PHP
  • 0 Dependents
  • 0 Suggesters
  • 0 Forks
  • 1 Open issues
  • 1 Versions
  • 0 % Grown

The README.md

Sylius Brand Plugin

Latest Version on Packagist ![Software License][ico-license] Build Status ![Quality Score][ico-code-quality], (*1)

, (*2)

If you want to add a brand to your products this is the plugin to use. Use cases: - Add brand logo to your product pages - Filter by brand in the frontend or backend, i.e. product lists, (*3)

Screenshots

CLICK TO SEE
, (*4)

Menu:, (*5)

Screenshot showing admin menu, (*6)

Brand admin pages:, (*7)

Screenshot showing brand admin index page, (*8)

Screenshot showing brand admin update page, (*9)

Screenshot showing brand admin media tab at update page, (*10)

Products admin pages:, (*11)

Screenshot showing product admin index page with brand filter, (*12)

Screenshot showing product admin index page with brand column, (*13)

Screenshot showing brand tab at product admin update page, (*14)

, (*15)

Installation

Step 1: Download the plugin

Open a command console, enter your project directory and execute the following command to download the latest stable version of this bundle:, (*16)

$ composer require loevgaard/sylius-brand-plugin

This command requires you to have Composer installed globally, as explained in the installation chapter of the Composer documentation., (*17)

Step 2: Enable the plugin

Then, enable the plugin by adding it to the list of registered plugins/bundles in config/bundles.php file of your project before (!) SyliusGridBundle:, (*18)

 ['all' => true],
    Sylius\Bundle\GridBundle\SyliusGridBundle::class => ['all' => true],
    // ...
];
```

### Step 3: Configure the plugin

```yaml
# config/packages/loevgaard_sylius_brand.yaml

imports:
    # ...
    - { resource: "@LoevgaardSyliusBrandPlugin/Resources/config/app/config.yaml" }

    # If you want to see Brand column at admin products list - uncomment next line
    # - { resource: "@LoevgaardSyliusBrandPlugin/Resources/config/grids/sylius_admin_product.yaml" }
```

```yaml
# config/routes/loevgaard_sylius_brand.yaml

loevgaard_sylius_brand:
    resource: "@LoevgaardSyliusBrandPlugin/Resources/config/routing.yaml"
```

### Step 4: Extend services and entities

#### Extend `Product`

- If you use `annotations` mapping:

    ```php
    

    <!-- config/doctrine/model/Product.orm.xml -->

    <doctrine-mapping xmlns="http://doctrine-project.org/schemas/orm/doctrine-mapping"
                      xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
                      xsi:schemaLocation="http://doctrine-project.org/schemas/orm/doctrine-mapping
                                          http://doctrine-project.org/schemas/orm/doctrine-mapping.xsd">

        <mapped-superclass name="App\Model\Product" table="sylius_product">
            <many-to-one field="brand" target-entity="Loevgaard\SyliusBrandPlugin\Model\BrandInterface" inversed-by="products">
                <join-column name="brand_id" on-delete="SET NULL" />
            </many-to-one>
        </mapped-superclass>

    </doctrine-mapping>
    ```

#### Extend `ProductRepository`

```php
<?php
# src/Doctrine/ORM/ProductRepository.php

declare(strict_types=1);

namespace App\Doctrine\ORM;

use Loevgaard\SyliusBrandPlugin\Doctrine\ORM\ProductRepositoryInterface as LoevgaardSyliusBrandPluginProductRepositoryInterface;
use Loevgaard\SyliusBrandPlugin\Doctrine\ORM\ProductRepositoryTrait as LoevgaardSyliusBrandPluginProductRepositoryTrait;
use Sylius\Bundle\CoreBundle\Doctrine\ORM\ProductRepository as BaseProductRepository;

class ProductRepository extends BaseProductRepository implements LoevgaardSyliusBrandPluginProductRepositoryInterface
{
    use LoevgaardSyliusBrandPluginProductRepositoryTrait;

    // ...
}

Configure

config/services.yaml

sylius_product:
    resources:
        product:
            classes:
                model: App\Model\Product # Or App\Entity\Product
                repository: App\Doctrine\ORM\ProductRepository

Step 5: Update your database schema

$ php bin/console doctrine:migrations:diff
$ php bin/console doctrine:migrations:migrate

Fixtures

  • Include prefedined brand fixtures to play with on your dev environment:, (*19)

    # config/packages/loevgaard_sylius_brand.yaml
    
    imports:
        - { resource: "@LoevgaardSyliusBrandPlugin/Resources/config/app/fixtures.yaml" }
    
  • Or write your own:, (*20)

    • Add a new yaml file to the folder config/packages and name it as you wish, e.g. my_own_fixtures.yaml., (*21)

    • Fill this yaml with your own brand fixtures and don't forget to declare the definition of your product(s) before this brand definition or use existing product(s) code., (*22)

    # config/packages/my_own_fixtures.yaml
    
    sylius_fixtures:
       suites:
           my_own_brand_fixtures:
                fixtures:
                    loevgaard_sylius_brand_plugin_brand:
                        options:
                            custom:
                                my_brand:
                                    name: 'My brand'
                                    code: 'my-brand'
                                    images:
                                        local_image:
                                            type: logo
                                            path: images/my-brand/logo.jpg
                                        3rd_party_plugin_image:
                                            type: black-and-white
                                            path: '@SomePlugin/Resources/images/my-brand/black-and-white.jpg'
                                    products:
                                      - product_code_1
                                      - product_code_2
                                      - product_code_3
    

    See example at src/Resources/config/app/fixtures.yaml., (*23)

    1. Load your fixtures
    php bin/console sylius:fixture:load my_own_brand_fixtures
    

Installation and usage for plugin development

To run test application to play with just type composer try., (*24)

Sonata blocks available

  • loevgaard_sylius_brand.admin.brand.create.tab_details
  • loevgaard_sylius_brand.admin.brand.update.tab_details
  • loevgaard_sylius_brand.admin.brand.create.tab_media
  • loevgaard_sylius_brand.admin.brand.update.tab_media

Events available

  • loevgaard_sylius_brand.menu.admin.brand.form to customize Brand admin form like you usually do with Product form via sylius.menu.admin.product.form event.

Testing

Play with API

  • Install https://stedolan.github.io/jq/ (type brew install jq at OSX terminal), (*25)

  • Get admin API access token:, (*26)

    SYLIUS_ADMIN_API_ACCESS_TOKEN=$(curl http://localhost:8000/api/oauth/v2/token \
        --silent --show-error \
        -d "client_id"=demo_client \
        -d "client_secret"=secret_demo_client \
        -d "grant_type"=password \
        -d "username"=api@example.com \
        -d "password"=sylius-api | jq '.access_token' --raw-output)
    
  • Make requests:, (*27)

    (GET requests indexing/showing resources shown here as an example, see tests/Controller/*ApiTest.php to discover more details about creating/updating/removing brand-related resources, creating new products with brand attached to it, uploading image files for BrandImages via API), (*28)

    To Brands admin API:, (*29)

    curl http://localhost:8000/api/v1/brands/ \
        -H "Authorization: Bearer $SYLIUS_ADMIN_API_ACCESS_TOKEN"
    
    curl http://localhost:8000/api/v1/brands/setono/ \
        -H "Authorization: Bearer $SYLIUS_ADMIN_API_ACCESS_TOKEN"
    

    Brand images API:, (*30)

    curl http://localhost:8000/api/v1/brands/setono/images/ \
        -H "Authorization: Bearer $SYLIUS_ADMIN_API_ACCESS_TOKEN"
    

    Brand images by type API:, (*31)

    curl http://localhost:8000/api/v1/brands/setono/images/logo/ \
        -H "Authorization: Bearer $SYLIUS_ADMIN_API_ACCESS_TOKEN"
    
    curl http://localhost:8000/api/v1/brands/setono/images/<ID>/ \
        -H "Authorization: Bearer $SYLIUS_ADMIN_API_ACCESS_TOKEN"
    

    Brand products API:, (*32)

    curl http://localhost:8000/api/v1/brands/setono/products/ \
        -H "Authorization: Bearer $SYLIUS_ADMIN_API_ACCESS_TOKEN"
    

    Product details API:, (*33)

    curl http://localhost:8000/api/v1/products/setono-mug/ \
        -H "Authorization: Bearer $SYLIUS_ADMIN_API_ACCESS_TOKEN"
    

    For XML output, add -H "Accept: application/xml" to request, (*34)

Contribute

Please, run composer all to run all checks and tests before committing., (*35)

Contribute by translating

We use the same service as Sylius for translating, namely Crowdin. You can help out by translating this project into your mother tongue ;), (*36)

Thanks!, (*37)

The Versions