From 42b3d7083b8324eb2af30772b48bf56978cb835f Mon Sep 17 00:00:00 2001 From: Jellygnite Date: Wed, 9 Dec 2020 15:49:38 +1000 Subject: [PATCH] added accordion element --- src/Model/AccordionObject.php | 78 ++++++++ src/Model/ElementAccordion.php | 177 ++++++++++++++++++ .../Elements/Includes/AccordionList.ss | 11 ++ .../Elements/Model/ElementAccordion.ss | 5 + 4 files changed, 271 insertions(+) create mode 100644 src/Model/AccordionObject.php create mode 100644 src/Model/ElementAccordion.php create mode 100644 templates/Jellygnite/Elements/Includes/AccordionList.ss create mode 100644 templates/Jellygnite/Elements/Model/ElementAccordion.ss diff --git a/src/Model/AccordionObject.php b/src/Model/AccordionObject.php new file mode 100644 index 0000000..796ed8e --- /dev/null +++ b/src/Model/AccordionObject.php @@ -0,0 +1,78 @@ + ElementAccordion::class, + ); + + /** + * @var string + */ + private static $table_name = 'AccordionObject'; + + /** + * @var array + */ + private static $summary_fields = [ + 'Summary', + ]; + + private static $defaults = array ( + 'ShowTitle' => '1' + ); + + + /** + * @return FieldList + * + * @throws \Exception + */ + public function getCMSFields() + { + $this->beforeUpdateCMSFields(function (FieldList $fields) { + $fields->removeByName('ElementAccordion'); + $fields->removeByName('Image'); + + + }); + + $fields = parent::getCMSFields(); + + return $fields; + } + + /** + * @return mixed + */ + public function getSummary() + { + return $this->dbObject('Content')->Summary(20); + } +} diff --git a/src/Model/ElementAccordion.php b/src/Model/ElementAccordion.php new file mode 100644 index 0000000..774745b --- /dev/null +++ b/src/Model/ElementAccordion.php @@ -0,0 +1,177 @@ + 'HTMLText' + ]; + + /** + * @var array + */ + private static $many_many = array( + 'Accordions' => AccordionObject::class, + ); + + /** + * @var array + */ + private static $many_many_extraFields = array( + 'Accordions' => array( + 'SortOrder' => 'Int', + ), + ); + + /** + * @var array + */ + private static $owns = [ + 'Accordions', + ]; + + /** + * 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; + + private static $defaults = array ( + 'ShowTitle' => '1' + ); + + /** + * @param bool $includerelations + * @return array + */ + public function fieldLabels($includerelations = true) + { + $labels = parent::fieldLabels($includerelations); + + $labels['Content'] = _t(__CLASS__.'.ContentLabel', 'Intro'); + $labels['Accordions'] = _t(__CLASS__ . '.AccordionsLabel', 'Accordions'); + + return $labels; + } + + /** + * @return FieldList + */ + public function getCMSFields() + { + $this->beforeUpdateCMSFields(function (FieldList $fields) { + $fields->dataFieldByName('Content') + ->setRows(5); + + if ($this->ID) { + /** @var \SilverStripe\Forms\GridField\GridField $AccordionField */ + $accordionField = $fields->dataFieldByName('Accordions'); + $fields->removeByName('Accordions'); + $config = $accordionField->getConfig(); + $config->removeComponentsByType([ + GridFieldAddExistingAutocompleter::class, + GridFieldDeleteAction::class, + GridFieldArchiveAction::class, + ])->addComponents( + new GridFieldOrderableRows('SortOrder'), + new GridFieldAddExistingSearchButton() + ); + + $fields->addFieldsToTab('Root.Main', array( + $accordionField, + )); + } + }); + + return parent::getCMSFields(); + } + + /** + * @return mixed + */ + public function getAccordionList() + { + return $this->Accordions()->sort('SortOrder'); + } + + /** + * @return DBHTMLText + */ + public function getSummary() + { + if ($this->Accordions()->count() == 1) { + $label = ' accordion'; + } else { + $label = ' accordions'; + } + return DBField::create_field('HTMLText', $this->Accordions()->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', 'Accordion'); + } +} diff --git a/templates/Jellygnite/Elements/Includes/AccordionList.ss b/templates/Jellygnite/Elements/Includes/AccordionList.ss new file mode 100644 index 0000000..9dbe6ec --- /dev/null +++ b/templates/Jellygnite/Elements/Includes/AccordionList.ss @@ -0,0 +1,11 @@ + \ No newline at end of file diff --git a/templates/Jellygnite/Elements/Model/ElementAccordion.ss b/templates/Jellygnite/Elements/Model/ElementAccordion.ss new file mode 100644 index 0000000..dd53d78 --- /dev/null +++ b/templates/Jellygnite/Elements/Model/ElementAccordion.ss @@ -0,0 +1,5 @@ +<% if $Title && $ShowTitle %>

$Title

<% end_if %> +<% if $Content %>
$Content
<% end_if %> +<% if $AccordionList %> + <% include Jellygnite/Elements/AccordionList %> +<% end_if %> \ No newline at end of file