Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added multi language support to portfolio bundle #14

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 22 additions & 8 deletions Admin/ProjectAdmin.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,15 +25,26 @@ public function __construct($code, $class, $baseControllerName)
protected function configureFormFields(FormMapper $formMapper)
{
$formMapper
->add('name')
->add('slug')
->with('General')
->add('name')
->add('slug')
->add('description')
->add('translations', 'project_translations', array(
'by_reference' => false,
'attr' => array(
'class' => 'project-translations',
),
'locales' => array('uk', 'en')
))

->add('url')
->add('description')
->add('imageFile', 'file', array('required' => false, 'data_class' => 'Symfony\Component\HttpFoundation\File\File'))
->add('date', 'date')
->add('categories')
->add('users')
->add('onFrontPage', 'checkbox', array('required' => false))
->with('Options')
->add('imageFile', 'file', array('required' => false, 'data_class' => 'Symfony\Component\HttpFoundation\File\File'))
->add('date', 'date')
->add('categories')
->add('users')
->add('onFrontPage', 'checkbox', array('required' => false))
->end();
;
}

Expand All @@ -44,6 +55,9 @@ protected function configureListFields(ListMapper $listMapper)
->addIdentifier('slug')
->add('name')
->add('date')
->add('translations', 'text', array(
'template' => 'StfalconPortfolioBundle:ProjectAdmin:list_translations_field.html.twig'
))
;
}

Expand Down
2 changes: 1 addition & 1 deletion Controller/CategoryController.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ class CategoryController extends Controller
*
* @return array
* @Route(
* "/portfolio/{slug}/{page}",
* "{slug}/{page}",
* name="portfolio_category_view",
* requirements={"page" = "\d+"},
* defaults={"page" = "1"}
Expand Down
2 changes: 1 addition & 1 deletion Controller/ProjectController.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ class ProjectController extends Controller
* @param string $projectSlug Slug of project
*
* @return array
* @Route("/portfolio/{categorySlug}/{projectSlug}", name="portfolio_project_view")
* @Route("/{categorySlug}/{projectSlug}", name="portfolio_project_view")
* @Template()
*/
public function viewAction($categorySlug, $projectSlug)
Expand Down
76 changes: 69 additions & 7 deletions Entity/Project.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,21 +3,24 @@
namespace Stfalcon\Bundle\PortfolioBundle\Entity;

use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\Common\Collections\Collection;
use Doctrine\ORM\Mapping as ORM;
use Gedmo\Mapping\Annotation as Gedmo;
use Gedmo\Translatable\Translatable;
use Imagine;
use Symfony\Component\HttpFoundation\File\UploadedFile;
use Symfony\Component\Validator\Constraints as Assert;
use Vich\UploaderBundle\Mapping\Annotation as Vich;
use Gedmo\Mapping\Annotation as Gedmo;
use Imagine;

/**
* Project entity
*
* @ORM\Table(name="portfolio_projects")
* @ORM\Entity(repositoryClass="Stfalcon\Bundle\PortfolioBundle\Repository\ProjectRepository")
* @Vich\Uploadable
* @Gedmo\TranslationEntity(class="Stfalcon\Bundle\PortfolioBundle\Entity\ProjectTranslation")
*/
class Project
class Project implements Translatable
{

/**
Expand All @@ -34,6 +37,7 @@ class Project
*
* @Assert\NotBlank()
* @Assert\MinLength(3)
* @Gedmo\Translatable(fallback=true)
* @ORM\Column(name="name", type="string", length=255)
*/
private $name;
Expand All @@ -52,6 +56,7 @@ class Project
*
* @Assert\NotBlank()
* @Assert\MinLength(10)
* @Gedmo\Translatable(fallback=true)
* @ORM\Column(name="description", type="text")
*/
private $description;
Expand Down Expand Up @@ -145,13 +150,35 @@ class Project
private $users;

/**
* Initialization properties for new project entity
* @var ArrayCollection
*
* @return void
* @ORM\OneToMany(targetEntity="ProjectTranslation", mappedBy="object", cascade={"persist", "remove"})
*/
protected $translations;

/**
* @var string $locale
*
* Required for Translatable behaviour
* @Gedmo\Locale
*/
protected $locale;

/**
* Initialization properties for new project entity
*/
public function __construct()
{
$this->categories = new ArrayCollection();
$this->categories = new ArrayCollection();
$this->translations = new ArrayCollection();
}

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

/**
Expand Down Expand Up @@ -189,7 +216,7 @@ public function addCategory(Category $category)
/**
* Set categories collection to project
*
* @param \Doctrine\Common\Collections\Collection $categories Categories collection
* @param Collection $categories Categories collection
*
* @return void
*/
Expand Down Expand Up @@ -486,4 +513,39 @@ public function getImageFile()
{
return $this->imageFile;
}

/**
* @return ArrayCollection
*/
public function getTranslations()
{
return $this->translations;
}

/**
* @param ProjectTranslation $projectTranslation
*/
public function addTranslation(ProjectTranslation $projectTranslation)
{
if (!$this->translations->contains($projectTranslation)) {
$this->translations->add($projectTranslation);
$projectTranslation->setObject($this);
}
}

/**
* @param ProjectTranslation $projectTranslation
*/
public function removeTranslation(ProjectTranslation $projectTranslation)
{
$this->translations->removeElement($projectTranslation);
}

/**
* @param Collection $translations
*/
public function setTranslations(Collection $translations)
{
$this->translations = $translations;
}
}
43 changes: 43 additions & 0 deletions Entity/ProjectTranslation.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
<?php

namespace Stfalcon\Bundle\PortfolioBundle\Entity;

use Doctrine\ORM\Mapping as ORM;
use Gedmo\Translatable\Entity\MappedSuperclass\AbstractPersonalTranslation;

/**
* @ORM\Entity
* @ORM\Table(name="project_translations", uniqueConstraints={
* @ORM\UniqueConstraint(name="lookup_unique_idx", columns={
* "locale", "object_id", "field"
* })
* })
*/
class ProjectTranslation extends AbstractPersonalTranslation
{
/**
* Convenient constructor
*
* @param string $locale
* @param string $field
* @param string $content
*/
public function __construct($locale = null, $field = null, $content = null)
{
$this->setLocale($locale);
$this->setField($field);
$this->setContent($content);
}

/**
* @ORM\ManyToOne(targetEntity="Project", inversedBy="translations")
* @ORM\JoinColumn(name="object_id", referencedColumnName="id", onDelete="CASCADE")
*/
protected $object;

function __toString()
{
return $this->getLocale();
}
}

55 changes: 55 additions & 0 deletions Form/TranslationLocaleType.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
<?php

namespace Stfalcon\Bundle\PortfolioBundle\Form;

use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\OptionsResolver\OptionsResolverInterface;

class TranslationLocaleType extends AbstractType
{
/**
* Builds the form.
*
* This method is called for each type in the hierarchy starting form the
* top most type. Type extensions can further modify the form.
*
* @see FormTypeExtensionInterface::buildForm()
*
* @param FormBuilderInterface $builder The form builder
* @param array $options The options
*/
public function buildForm(FormBuilderInterface $builder, array $options)
{
foreach ($options['fields'] as $field => $type) {
$builder->add($field, $type, array(
'label' => ucfirst($field),
'required' => false,
'attr' => array(
'class' => 'span5'
)
));
}
}

/**
* @param OptionsResolverInterface $resolver
*/
public function setDefaultOptions(OptionsResolverInterface $resolver)
{
$resolver->setDefaults(array(
'error_bubbling' => true,
'fields' => array(),
));
}

/**
* Get form name
*
* @return string
*/
public function getName()
{
return 'project_translation_locale';
}
}
Loading