PHP code example of heimrichhannot / contao-syndication-type-bundle

1. Go to this page and download the library: Download heimrichhannot/contao-syndication-type-bundle library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

heimrichhannot / contao-syndication-type-bundle example snippets


namespace Acme\ExampleBundle\EventListener;

use HeimrichHannot\SyndicationTypeBundle\Event\BeforeRenderSyndicationLinksEvent;
use Terminal42\ServiceAnnotationBundle\Annotation\ServiceTag;

/**
 * @ServiceTag("kernel.event_listener", event="HeimrichHannot\SyndicationTypeBundle\Event\BeforeRenderSyndicationLinksEvent")
 */
class SyndicationBeforeRenderSyndicationLinksEventListener
{
    public function __invoke(BeforeRenderSyndicationLinksEvent $event): void
    {
        $options = $event->getLinkRenderOptions();
        $options['template'] = 'syndication_link_acme';
        $event->setLinkRenderOptions($options);
    }
}

use HeimrichHannot\SyndicationTypeBundle\SyndicationLink\SyndicationLinkRenderer;

class DecoratedLinkRenderer extends SyndicationLinkRenderer
{
    protected SyndicationLinkRenderer $inner;

    public function __construct(SyndicationLinkRenderer $inner)
    {
        $this->inner = $inner;
    }

    public function renderProvider(SyndicationLinkProvider $provider, array $options = []): string
    {
        // Tell the renderProvider method to call the customized render method
        return $this->inner->renderProvider($provider, array_merge($options, [
            'render_callback' => [$this, 'render']
        ]));
    }

    public function render(SyndicationLink $link, array $options = []): string
    {
        // add or customize link attributes
        $options['attributes']['class'] = trim(($options['attributes']['class']  ?? '').' btn btn-primary');
        // don't output template dev comments
        $options['disable_dev_comments'] = true;
        // a custom template (pass only the name)
        $options['template'] = 'a_really_custom_link_template';
        // override the link content
        $options['content'] = "Click THIS link!";
       
        return $this->inner->render($link, $options);
    }
}

    use HeimrichHannot\SyndicationTypeBundle\Dca\SyndicationTypeDcaProvider;
    
    function prepareTable(string $table, SyndicationTypeDcaProvider $syndicationTypeDcaProvider)
      {
          $dca = &$GLOBALS['TL_DCA']['tl_article'];
          $dca['palettes']['default'] = str_replace(
            'printable', 
            $syndicationTypeDcaProvider->getPalette(false), 
            $dca['palettes']['default']
          );
          $syndicationTypeDcaProvider->prepareDca($table);
      }
    

    use HeimrichHannot\SyndicationTypeBundle\Manager\SyndicationManager;
    
    class ExampleController {
    
        private SyndicationManager               $syndicationManager;
    
        public function addSyndication(string $title, string $content, array $itemData, ExampleModel $config): string
        {
            // $title: A title for the syndication
            // $content: What should be shared. Can be a teaser text or a rendered template.
            // $itemData: The data of the entity to share
            // $config: The configuration. Typical the model data (row) of the configuration. Maybe the same as item data.
            $context = $this->syndicationManager->createContext($title, $content, $itemData, $config->row());
            
            // See next paragraph
            $this->doExport($context);
            
            $provider = $this->syndicationManager->getLinkProviderGenerator()->generateFromContext($context);
            $rendererContext = $this->syndicationManager->createLinkRendererContextFromModel();
            
            return $this->syndicationManager->getLinkRenderer()->renderProvider($provider, $rendererContext);
        }
    
    }
    

    use HeimrichHannot\SyndicationTypeBundle\Manager\SyndicationManager;
    use HeimrichHannot\SyndicationTypeBundle\SyndicationContext\SyndicationContext;
        
    class ExampleController {
    
        private SyndicationManager               $syndicationManager;
        
        private function doExport(SyndicationContext $syndicationContext): void
        {
            $this->syndicationManager->getExportSyndicationHandler()->exportByContext($context);
        }
        
    }