Source of file ElementSponsor.php
Size: 3,740 Bytes - Last Modified: 2021-12-24T06:46:03+00:00
/var/www/docs.ssmods.com/process/src/src/Elements/ElementSponsor.php
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155 | <?php namespace Dynamic\Elements\Sponsors\Elements; use DNADesign\Elemental\Models\BaseElement; use Dynamic\Elements\Sponsors\Model\Sponsor; use SilverStripe\Forms\FieldList; use SilverStripe\Forms\GridField\GridFieldAddExistingAutocompleter; use SilverStripe\ORM\FieldType\DBField; use Symbiote\GridFieldExtensions\GridFieldAddExistingSearchButton; use Symbiote\GridFieldExtensions\GridFieldOrderableRows; /** * Class ElementSponsor * @package Dynamic\Elements\Sponsors\Elements * * @property int $Limit * @property string $Content * @method \SilverStripe\ORM\ManyManyList Sponsors() */ class ElementSponsor extends BaseElement { /** * @var string */ private static $icon = 'font-icon-external-link'; /** * @var string */ private static $singular_name = 'Sponsors Element'; /** * @var string */ private static $plural_name = 'Sponsors Elements'; /** * @var string */ private static $table_name = 'ElementSponsor'; /** * @var array */ private static $db = array( 'Limit' => 'Int', 'Content' => 'HTMLText', ); /** * @var array */ private static $many_many = [ 'Sponsors' => Sponsor::class, ]; /** * @var array */ private static $many_many_extraFields = [ 'Sponsors' => [ // would be sort but issues arise when parent and child both have the same sort field names. 'SponsorSort' => 'Int', ], ]; /** * @var array */ private static $defaults = array( 'Limit' => 0, ); /** * Set to false to prevent an in-line edit form from showing in an elemental area. Instead the element will be * clickable and a GridFieldDetailForm will be used. * * @config * @var bool */ private static $inline_editable = false; /** * @return string */ public function getSummary() { if ($this->Sponsors()->count() == 1) { $label = ' sponsor'; } else { $label = ' sponsors'; } return DBField::create_field('HTMLText', $this->Sponsors()->count() . $label)->Summary(20); } /** * @return array */ protected function provideBlockSchema() { $blockSchema = parent::provideBlockSchema(); $blockSchema['content'] = $this->getSummary(); return $blockSchema; } /** * @return string */ public function getType() { return _t(__CLASS__.'.BlockType', 'Sponsors'); } /** * @return FieldList */ public function getCMSFields() { $this->beforeUpdateCMSFields( function (FieldList $fields) { $fields->dataFieldByName('Content') ->setRows(8); $fields->dataFieldByName('Limit') ->setTitle('Number of sponsors to show') ->setDescription('0 will show all sponsors'); if ($this->exists()) { $config = $fields->dataFieldByName('Sponsors')->getConfig(); $config->removeComponentsByType(GridFieldAddExistingAutocompleter::class); $config->addComponent(new GridFieldAddExistingSearchButton()); $config->addComponent(new GridFieldOrderableRows('SponsorSort')); } } ); return parent::getCMSFields(); } /** * @return mixed */ public function getSponsorsList() { $list = $this->Sponsors()->sort('SponsorSort'); if ($this->Limit > 0) { $list = $list->limit($this->Limit); } return $list; } } |