Skip to content

Commit

Permalink
added accordion element
Browse files Browse the repository at this point in the history
  • Loading branch information
jellygnite committed Dec 9, 2020
1 parent b6c221f commit 42b3d70
Show file tree
Hide file tree
Showing 4 changed files with 271 additions and 0 deletions.
78 changes: 78 additions & 0 deletions src/Model/AccordionObject.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
<?php
namespace Jellygnite\Elements\Model;

use Jellygnite\Elements\Model\BaseElementObject;
use Jellygnite\Elements\Model\ElementAccordion;
use SilverStripe\Forms\FieldList;
use SilverStripe\Forms\GridField\GridField;
use SilverStripe\Forms\GridField\GridFieldConfig_RecordViewer;

/**
* Class AccordionObject
*
* @method \SilverStripe\ORM\ManyManyList ElementAccordions()
*/
class AccordionObject extends BaseElementObject
{

/**
* @return string
*/
private static $singular_name = 'Accordion';

/**
* @return string
*/
private static $plural_name = 'Accordions';

/**
* @var array
*/
private static $belongs_many_many = array(
'ElementAccordion' => 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);
}
}
177 changes: 177 additions & 0 deletions src/Model/ElementAccordion.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,177 @@
<?php

namespace Jellygnite\Elements\Model;

use DNADesign\Elemental\Models\BaseElement;
use Jellygnite\Elements\Model\AccordionObject;
use SilverStripe\Forms\FieldList;
use SilverStripe\Forms\GridField\GridFieldAddExistingAutocompleter;
use SilverStripe\ORM\FieldType\DBField;
use SilverStripe\ORM\FieldType\DBHTMLText;
use SilverStripe\Versioned\GridFieldArchiveAction;
use Symbiote\GridFieldExtensions\GridFieldAddExistingSearchButton;
use Symbiote\GridFieldExtensions\GridFieldOrderableRows;

/**
* Class ElementAccordion
*
* @property string $Content
*
* @method \SilverStripe\ORM\ManyManyList Accordion()
*/
class ElementAccordion extends BaseElement
{

/**
* @var string
*/
private static $icon = 'font-icon-block-accordion';

/**
* @return string
*/
private static $singular_name = 'Accordion Element';

/**
* @return string
*/
private static $plural_name = 'Accordion Elements';

/**
* @var string
*/
private static $table_name = 'ElementAccordion';

/**
* @var array
*/
private static $styles = [];

/**
* @var array
*/
private static $db = [
'Content' => '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');
}
}
11 changes: 11 additions & 0 deletions templates/Jellygnite/Elements/Includes/AccordionList.ss
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<ul class="list-accordion" data-uk-accordion>
<% loop $AccordionList %>
<li class="item">
<div class="uk-accordion-title">$Title</div>
<div class="uk-accordion-content">
<% if $Content %><div class="item-content">$Content</div><% end_if %>
<% with $ElementLink %><% if $LinkURL %><div class="item-link"><a href="$LinkURL"{$TargetAttr} class="$Classes btn btn-secondary">$Title</a></div><% end_if %><% end_with %>
</div>
</li>
<% end_loop %>
</ul>
5 changes: 5 additions & 0 deletions templates/Jellygnite/Elements/Model/ElementAccordion.ss
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<% if $Title && $ShowTitle %><h2 class="element__title text-primary">$Title</h2><% end_if %>
<% if $Content %><div class="element__content uk-h6 text-tertiary my-0">$Content</div><% end_if %>
<% if $AccordionList %>
<% include Jellygnite/Elements/AccordionList %>
<% end_if %>

0 comments on commit 42b3d70

Please sign in to comment.