2017 © Pedro Peláez
 

contao-module events-create-options

options_callback event and helpers for Contao Open Source CMS

image

contao-community-alliance/events-create-options

options_callback event and helpers for Contao Open Source CMS

  • Wednesday, June 6, 2018
  • by xtra
  • Repository
  • 7 Watchers
  • 0 Stars
  • 2,994 Installations
  • PHP
  • 29 Dependents
  • 0 Suggesters
  • 0 Forks
  • 0 Open issues
  • 4 Versions
  • 3 % Grown

The README.md

Events: Create options

Event and helper classes to provide option_callback's via events., (*1)

In your DCA, define the options_callback with the factory class CreateOptionsEventCallbackFactory., (*2)

use ContaoCommunityAlliance\Contao\Events\CreateOptions\CreateOptionsEventCallbackFactory;

$GLOBALS['TL_DCA']['tl_foo']['fields']['some_select'] = array(
    'inputType' => 'select',
    ...
    'options_callback' => CreateOptionsEventCallbackFactory::createCallback('tl_foo.some_select.create-options'),
);

Now you can fill the options with an event listener, listening on the event named tl_foo.some_select.create-options., (*3)

$GLOBALS['TL_EVENTS']['tl_foo.some_select.create-options'][] = function($event) {
    $options = $event->getOptions();

    $options['value1'] = 'label 1';
    $options['value2'] = 'label 2';
    $options['value3'] = 'label 3';
};

Manipulate the options with a second event listener is pretty easy., (*4)

$GLOBALS['TL_EVENTS']['tl_foo.some_select.create-options'][] = array(
    function($event) {
        $options = $event->getOptions();

        // remove a default value
        unset($options['value2']);

        // add a new value
        $options['value4'] = 'label 4';
    },
    -10 // we need a lower priority here, to make sure this listener is triggered after the default listener
);

See the event dispatcher documentation for more examples how to listen on an event., (*5)

Custom event

By default, an event of type ContaoCommunityAlliance\Contao\Events\CreateOptions\CreateOptionsEvent is used. If you want your own event type, you can pass the class or a factory method as second parameter to CreateOptionsEventCallbackFactory::createCallback()., (*6)

First you need to write your own create-options event class., (*7)

class MyCreateOptionsEvent extends \ContaoCommunityAlliance\Contao\Events\CreateOptions\CreateOptionsEvent
{
    protected $additionalData;

    function __construct($additionalData, \DataContainer $dataContainer, \ArrayObject $options = null)
    {
        parent::__construct($dataContainer, $options);
        $this->additionalData = $additionalData;
    }

    public function getAdditionalData()
    {
        return $this->additionalData;
    }
}

Then you need to add your factory to CreateOptionsEventCallbackFactory::createCallback()., (*8)

use ContaoCommunityAlliance\Contao\Events\CreateOptions\CreateOptionsEventCallbackFactory;

$GLOBALS['TL_DCA']['tl_foo']['fields']['some_select'] = array(
    'inputType' => 'select',
    ...
    'options_callback' => CreateOptionsEventCallbackFactory::createCallback(
        'tl_foo.some_select.create-options',
        function($dataContainer) {
            return new \MyCreateOptionsEvent(array('some' => 'value'), $dc);
        }
    ),
);

The Versions

06/06 2018

dev-develop

dev-develop

options_callback event and helpers for Contao Open Source CMS

  Sources   Download

LGPL-3.0+ LGPL-3.0-or-later

The Requires

 

event contao options_callback

06/06 2018

dev-master

9999999-dev

options_callback event and helpers for Contao Open Source CMS

  Sources   Download

LGPL-3.0+ LGPL-3.0-or-later

The Requires

 

event contao options_callback

06/06 2018

1.0.1

1.0.1.0

options_callback event and helpers for Contao Open Source CMS

  Sources   Download

LGPL-3.0-or-later

The Requires

 

event contao options_callback

05/06 2014

1.0

1.0.0.0

options_callback event and helpers for Contao Open Source CMS

  Sources   Download

LGPL-3.0+

The Requires

 

event contao options_callback