Browse Source

Référencement : metas

feature/symfony6.1
Guillaume 2 years ago
parent
commit
ff022cc0ce
11 changed files with 95 additions and 44 deletions
  1. +12
    -1
      Component/FileComponent.php
  2. +46
    -14
      Component/MetaComponent.php
  3. +2
    -0
      Controller/AbstractAdminController.php
  4. +8
    -6
      Definition/Field/AbstractFieldDefinition.php
  5. +1
    -1
      Definition/Field/Site/PageFieldDefinition.php
  6. +4
    -1
      Doctrine/Pattern/AbstractFullEntity.php
  7. +4
    -3
      Model/Site/NewsInterface.php
  8. +1
    -3
      Model/Site/NewsModel.php
  9. +1
    -1
      Resources/translations/admin.fr.yaml
  10. +15
    -9
      Twig/MetaTwigExtension.php
  11. +1
    -5
      Twig/TwigExtension.php

+ 12
- 1
Component/FileComponent.php View File



use Liip\ImagineBundle\Imagine\Cache\CacheManager; use Liip\ImagineBundle\Imagine\Cache\CacheManager;
use Symfony\Component\DependencyInjection\ParameterBag\ParameterBagInterface; use Symfony\Component\DependencyInjection\ParameterBag\ParameterBagInterface;
use Symfony\Component\Routing\Generator\UrlGeneratorInterface;


class FileComponent class FileComponent
{ {
protected ParameterBagInterface $parameterBag; protected ParameterBagInterface $parameterBag;
protected CacheManager $liipCacheHelper; protected CacheManager $liipCacheHelper;
protected UrlGeneratorInterface $router;


public function __construct(ParameterBagInterface $parameterBag, CacheManager $liipCacheHelper)
public function __construct(ParameterBagInterface $parameterBag, CacheManager $liipCacheHelper, UrlGeneratorInterface $router)
{ {
$this->parameterBag = $parameterBag; $this->parameterBag = $parameterBag;
$this->liipCacheHelper = $liipCacheHelper; $this->liipCacheHelper = $liipCacheHelper;
$this->router = $router;
}

public function getAssetUrl($path)
{
$context = $this->router->getContext();
$host = $context->getScheme().'://'.$context->getHost().'/';

return $host.$path;
} }


/** /**

+ 46
- 14
Component/MetaComponent.php View File



class MetaComponent class MetaComponent
{ {
protected FileComponent $fileComponent;
protected StringComponent $stringComponent;


public function getMetaTitle($entity)
public function __construct(FileComponent $fileComponent, StringComponent $stringComponent)
{
$this->fileComponent = $fileComponent;
$this->stringComponent = $stringComponent;
}

public function getMetaTitle($entity, $title = null)
{ {
if($entity) { if($entity) {
if(method_exists($entity, 'getMetaTitle')) {
if(method_exists($entity, 'getMetaTitle') && $entity->getMetaTitle()) {
return $entity->getMetaTitle() ; return $entity->getMetaTitle() ;
} }
elseif(method_exists($entity, 'getTitle')) {
elseif(!is_null($title)) {
return $title;
}
elseif(method_exists($entity, 'getTitle') && $entity->getTitle()) {
return $entity->getTitle() ; return $entity->getTitle() ;
} }
} }
public function getMetaDescription($entity) public function getMetaDescription($entity)
{ {
if($entity) { if($entity) {
if(method_exists($entity, 'getMetaDescription')) {
if(method_exists($entity, 'getMetaDescription') && $entity->getMetaDescription()) {
return $entity->getMetaDescription() ; return $entity->getMetaDescription() ;
} }
elseif(method_exists($entity, 'getDescription')) {
return $entity->getDescription() ;
elseif(method_exists($entity, 'getDescription') && $entity->getDescription()) {
return $this->formatDescription($entity->getDescription());
} }
} }


return '' ; return '' ;
} }


public function getOpenGraphTitle($entity)
public function getOpenGraphTitle($entity, $title = null)
{ {
if($entity) { if($entity) {
if(method_exists($entity, 'getOpenGraphTitle')) {
if(method_exists($entity, 'getOpenGraphTitle') && $entity->getOpenGraphTitle()) {
return $entity->getOpenGraphTitle() ; return $entity->getOpenGraphTitle() ;
} }
elseif(method_exists($entity, 'getTitle')) {
elseif(!is_null($title)) {
return $title;
}
elseif(method_exists($entity, 'getTitle') && $entity->getTitle()) {
return $entity->getTitle() ; return $entity->getTitle() ;
} }
} }
public function getOpenGraphDescription($entity) public function getOpenGraphDescription($entity)
{ {
if($entity) { if($entity) {
if(method_exists($entity, 'getOpenGraphDescription')) {
if(method_exists($entity, 'getOpenGraphDescription') && $entity->getOpenGraphDescription()) {
return $entity->getOpenGraphDescription() ; return $entity->getOpenGraphDescription() ;
} }
elseif(method_exists($entity, 'getDescription')) {
return $entity->getDescription() ;
elseif(method_exists($entity, 'getDescription') && $entity->getDescription()) {
return $this->formatDescription($entity->getDescription());
} }
} }


public function getOpenGraphImage($entity) public function getOpenGraphImage($entity)
{ {
if($entity) { if($entity) {
if(method_exists($entity, 'getOpenGraphImage')) {
if(method_exists($entity, 'getOpenGraphImage') && $entity->getOpenGraphImage()) {
return $entity->getOpenGraphImage() ; return $entity->getOpenGraphImage() ;
} }
elseif(method_exists($entity, 'getImage')) {
elseif(method_exists($entity, 'getImage') && $entity->getImage()) {
return $entity->getImage() ; return $entity->getImage() ;
} }
} }
return '' ; return '' ;
} }


public function getOpenGraphImageUrl($entity)
{
$image = $this->getOpenGraphImage($entity);
if($image && $image->getPath() && strlen($image->getPath()) > 0) {
return $this->fileComponent->getAssetUrl($image->getPath());
}

return '';
}

public function formatDescription($description)
{
$description = trim($description);
$description = $this->stringComponent->limitText($description, 50);
$description = str_replace("\r\n","",$description);

return $description;
}
} }

+ 2
- 0
Controller/AbstractAdminController.php View File

use Lc\SovBundle\Component\EntityComponent; use Lc\SovBundle\Component\EntityComponent;
use Lc\SovBundle\Definition\ActionDefinition; use Lc\SovBundle\Definition\ActionDefinition;
use Lc\SovBundle\Doctrine\Extension\DevAliasInterface; use Lc\SovBundle\Doctrine\Extension\DevAliasInterface;
use Lc\SovBundle\Doctrine\Extension\OpenGraphInterface;
use Lc\SovBundle\Doctrine\Extension\SeoInterface; use Lc\SovBundle\Doctrine\Extension\SeoInterface;
use Lc\SovBundle\Doctrine\Extension\SortableInterface; use Lc\SovBundle\Doctrine\Extension\SortableInterface;
use Lc\SovBundle\Doctrine\Extension\StatusInterface; use Lc\SovBundle\Doctrine\Extension\StatusInterface;
use Lc\SovBundle\Doctrine\Extension\TreeInterface; use Lc\SovBundle\Doctrine\Extension\TreeInterface;
use Lc\SovBundle\Field\CollectionField; use Lc\SovBundle\Field\CollectionField;
use Lc\SovBundle\Field\Filter\FilterManager; use Lc\SovBundle\Field\Filter\FilterManager;
use Lc\SovBundle\Field\ImageManagerField;
use Lc\SovBundle\Form\Common\FiltersFormType; use Lc\SovBundle\Form\Common\FiltersFormType;
use Lc\SovBundle\Form\Common\PositionType; use Lc\SovBundle\Form\Common\PositionType;
use Lc\SovBundle\Model\User\UserInterface; use Lc\SovBundle\Model\User\UserInterface;

+ 8
- 6
Definition/Field/AbstractFieldDefinition.php View File

'metaDescription' => TextareaField::new('metaDescription') 'metaDescription' => TextareaField::new('metaDescription')
->setLabel('Meta : description') ->setLabel('Meta : description')
->setHelp('Affiché dans les résultats de recherche Google'), ->setHelp('Affiché dans les résultats de recherche Google'),
'openGraphTitle' => TextField::new('openGraphTitle')
->setLabel('OpenGraph : titre'),
'openGraphDescription' => TextareaField::new('openGraphDescription')
->setLabel('OpenGraph : description'),
'openGraphImage' => ImageManagerField::new('openGraphImage')
->setLabel('OpenGraph : image'),
'oldUrls' => CollectionField::new('oldUrls') 'oldUrls' => CollectionField::new('oldUrls')
->setFormTypeOption('entry_type', TextType::class) ->setFormTypeOption('entry_type', TextType::class)
->setLabel('Anciennes urls du document') ->setLabel('Anciennes urls du document')
->hideOnIndex(), ->hideOnIndex(),
'openGraphTitle' => TextField::new('openGraphTitle')
->setLabel('OpenGraph : titre')
->setHelp('Utilisé par les réseaux sociaux pour récupérer le titre de la page'),
'openGraphDescription' => TextareaField::new('openGraphDescription')
->setLabel('OpenGraph : description')
->setHelp('Utilisé par les réseaux sociaux pour récupérer la description de la page'),
'openGraphImage' => ImageManagerField::new('openGraphImage')
->setLabel('OpenGraph : image'),
'devAlias' => TextField::new('devAlias')->hideOnIndex(), 'devAlias' => TextField::new('devAlias')->hideOnIndex(),
'status' => StatusField::new('status')->setSortable(true), 'status' => StatusField::new('status')->setSortable(true),
'createdAt' => DateTimeField::new('createdAt')->setSortable(true), 'createdAt' => DateTimeField::new('createdAt')->setSortable(true),

+ 1
- 1
Definition/Field/Site/PageFieldDefinition.php View File



public function configurePanels(): array public function configurePanels(): array
{ {
return ['general', 'seo', 'conf'];
return ['general', 'seo', 'opengraph', 'conf'];
} }


public function configureFields(): array public function configureFields(): array

+ 4
- 1
Doctrine/Pattern/AbstractFullEntity.php View File

use Lc\SovBundle\Doctrine\Extension\BlameableTrait; use Lc\SovBundle\Doctrine\Extension\BlameableTrait;
use Lc\SovBundle\Doctrine\Extension\DevAliasInterface; use Lc\SovBundle\Doctrine\Extension\DevAliasInterface;
use Lc\SovBundle\Doctrine\Extension\DevAliasTrait; use Lc\SovBundle\Doctrine\Extension\DevAliasTrait;
use Lc\SovBundle\Doctrine\Extension\OpenGraphInterface;
use Lc\SovBundle\Doctrine\Extension\OpenGraphTrait;
use Lc\SovBundle\Doctrine\Extension\SeoInterface; use Lc\SovBundle\Doctrine\Extension\SeoInterface;
use Lc\SovBundle\Doctrine\Extension\SeoTrait; use Lc\SovBundle\Doctrine\Extension\SeoTrait;
use Lc\SovBundle\Doctrine\Extension\SluggableInterface; use Lc\SovBundle\Doctrine\Extension\SluggableInterface;
/** /**
* @ORM\MappedSuperclass * @ORM\MappedSuperclass
*/ */
abstract class AbstractFullEntity implements BlameableInterface, SeoInterface, SluggableInterface, SortableInterface,
abstract class AbstractFullEntity implements BlameableInterface, SeoInterface, OpenGraphInterface, SluggableInterface, SortableInterface,
StatusInterface, TimestampableInterface, DevAliasInterface, EntityInterface StatusInterface, TimestampableInterface, DevAliasInterface, EntityInterface
{ {


use BlameableTrait; use BlameableTrait;
use SeoTrait; use SeoTrait;
use OpenGraphTrait;
use SluggableTrait; use SluggableTrait;
use SortableTrait; use SortableTrait;
use StatusTrait; use StatusTrait;

+ 4
- 3
Model/Site/NewsInterface.php View File



namespace Lc\SovBundle\Model\Site; namespace Lc\SovBundle\Model\Site;


use Lc\SovBundle\Doctrine\Extension\OpenGraphInterface;
use Lc\SovBundle\Model\File\FileInterface; use Lc\SovBundle\Model\File\FileInterface;
use Lc\SovBundle\Model\File\FileModel; use Lc\SovBundle\Model\File\FileModel;
use Lc\SovBundle\Model\Newsletter\NewsletterInterface; use Lc\SovBundle\Model\Newsletter\NewsletterInterface;


public function getOpenGraphTitle(): ?string; public function getOpenGraphTitle(): ?string;


public function setOpenGraphTitle(string $openGraphTitle): NewsInterface;
public function setOpenGraphTitle(string $openGraphTitle): OpenGraphInterface;


public function getOpenGraphDescription(): ?string; public function getOpenGraphDescription(): ?string;


public function setOpenGraphDescription(?string $openGraphDescription): NewsInterface;
public function setOpenGraphDescription(?string $openGraphDescription): OpenGraphInterface;


public function getOpenGraphImage(): ?FileInterface; public function getOpenGraphImage(): ?FileInterface;


public function setOpenGraphImage(?FileInterface $openGraphImage): NewsInterface;
public function setOpenGraphImage(?FileInterface $openGraphImage): OpenGraphInterface;


public function setMetaTitle(?string $metaTitle); public function setMetaTitle(?string $metaTitle);



+ 1
- 3
Model/Site/NewsModel.php View File

/** /**
* @ORM\MappedSuperclass() * @ORM\MappedSuperclass()
*/ */
abstract class NewsModel extends AbstractFullEntity implements NewsInterface, OpenGraphInterface
abstract class NewsModel extends AbstractFullEntity implements NewsInterface
{ {
use OpenGraphTrait;

/** /**
* @ORM\Column(type="datetime") * @ORM\Column(type="datetime")
* @Gedmo\Timestampable(on="create") * @Gedmo\Timestampable(on="create")

+ 1
- 1
Resources/translations/admin.fr.yaml View File

conf: Configuration conf: Configuration
gallery: Galerie gallery: Galerie
seo: Référencement seo: Référencement
opengraph: Opengraph
opengraph: Réseaux sociaux
flashes: flashes:
success: success:
created: L'élément a bien été créé created: L'élément a bien été créé

+ 15
- 9
Twig/MetaTwigExtension.php View File

public function getFunctions() public function getFunctions()
{ {
return [ return [
new TwigFunction('sov_meta_title', [$this, 'getMetaTitle']),
new TwigFunction('sov_meta_description', [$this, 'getMetaDescription']),
new TwigFunction('sov_opengraph_title', [$this, 'getOpengraphTitle']),
new TwigFunction('sov_opengraph_description', [$this, 'getOpengraphDescription']),
new TwigFunction('sov_opengraph_image', [$this, 'getOpengraphImage']),
new TwigFunction('meta_title', [$this, 'getMetaTitle']),
new TwigFunction('meta_description', [$this, 'getMetaDescription']),
new TwigFunction('opengraph_title', [$this, 'getOpengraphTitle']),
new TwigFunction('opengraph_description', [$this, 'getOpengraphDescription']),
new TwigFunction('opengraph_image', [$this, 'getOpengraphImage']),
new TwigFunction('opengraph_image_url', [$this, 'getOpengraphImageUrl']),
]; ];
} }


return []; return [];
} }


public function getMetaTitle($entity): ?string
public function getMetaTitle($entity, $title = null): ?string
{ {
return $this->metaComponent->getMetaTitle($entity);
return $this->metaComponent->getMetaTitle($entity, $title = null);
} }


public function getMetaDescription($entity): ?string public function getMetaDescription($entity): ?string
return $this->metaComponent->getMetaDescription($entity); return $this->metaComponent->getMetaDescription($entity);
} }


public function getOpenGraphTitle($entity): ?string
public function getOpenGraphTitle($entity, $title = null): ?string
{ {
return $this->metaComponent->getOpenGraphTitle($entity);
return $this->metaComponent->getOpenGraphTitle($entity, $title);
} }


public function getOpenGraphDescription($entity): ?string public function getOpenGraphDescription($entity): ?string
{ {
return $this->metaComponent->getOpenGraphImage($entity); return $this->metaComponent->getOpenGraphImage($entity);
} }

public function getOpenGraphImageUrl($entity): ?string
{
return $this->metaComponent->getOpenGraphImageUrl($entity);
}
} }

+ 1
- 5
Twig/TwigExtension.php View File



public function getAssetUrl($path) public function getAssetUrl($path)
{ {
$context = $this->router->getContext();
$host = $context->getScheme().'://'.$context->getHost().'/';

return $host.$path;
return $this->fileComponent->getAssetUrl($path);
} }



public function rot13(string $string): string public function rot13(string $string): string
{ {
return str_rot13($string); return str_rot13($string);

Loading…
Cancel
Save