Skip to content

Commit

Permalink
Merge pull request #20 from chrometoasters/pulls/tagging-gridfield-co…
Browse files Browse the repository at this point in the history
…nfig-reuse

Extract tagging gridfield configuration into a provider for reusability
  • Loading branch information
michalkleiner authored Mar 16, 2020
2 parents 233b3c1 + 101f103 commit 98f2bf8
Show file tree
Hide file tree
Showing 2 changed files with 82 additions and 41 deletions.
51 changes: 10 additions & 41 deletions src/Extensions/DataObjectTaxonomiesDataExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,7 @@

namespace Chrometoaster\AdvancedTaxonomies\Extensions;

use Chrometoaster\AdvancedTaxonomies\Forms\GridFieldAddTagsAutocompleter;
use Chrometoaster\AdvancedTaxonomies\Forms\GridFieldInfoLink;
use Chrometoaster\AdvancedTaxonomies\Forms\GridFieldOrderableRows;
use Chrometoaster\AdvancedTaxonomies\Forms\FieldsProvider;
use Chrometoaster\AdvancedTaxonomies\Models\DataObjectTaxonomyTerm;
use Chrometoaster\AdvancedTaxonomies\Models\TaxonomyTerm;
use Chrometoaster\AdvancedTaxonomies\Validators\TaxonomyRulesValidator;
Expand Down Expand Up @@ -61,48 +59,19 @@ public function updateCMSFields(FieldList $fields)
return;
}

// Remove config components from the Tags gridfield to disallow adding/deleting/archiving taxonomy terms from here
$components = GridFieldConfig_RelationEditor::create();
$components->removeComponentsByType(GridFieldAddNewButton::class);
$components->removeComponentsByType(GridFieldEditButton::class);
$components->removeComponentsByType(GridFieldArchiveAction::class);
$components->removeComponentsByType(GridFieldFilterHeader::class);

// Shift the GridFieldAddExistingAutocompleter component to left
$components->removeComponentsByType(GridFieldAddExistingAutocompleter::class);
$components->addComponent(
$addExisting = new GridFieldAddTagsAutocompleter('buttons-before-left')
);

$components->addComponent(
new GridFieldInfoLink('buttons-before-left', '/at-taxonomy-overview', "Open 'All taxonomies' overview")
);

$components->getComponentByType(GridFieldDataColumns::class)->setDisplayFields(
[
'getNameAsTagWithExtraInfo' => 'Name',
'getDescription15Words' => 'Description',
'getAllRequiredTypesNames' => 'Requires',
]
);

$components->addComponent(GridFieldOrderableRows::create('Sort'));

$autoResultFormat = ' {$getTermHierarchy} ';
$addExisting->setResultsFormat($autoResultFormat);
$addExisting->setPlaceholderText('Add tags by name');

// create a list of term candidates, excluding single select types which already have a tag in the list
// - this works in real time as adding a tag adds it to the list, even when unsaved
// a list of candidates for the add existing search field
// - excluding single select types which already have a tag in the list
// - this works in real time as adding a tag adds it to the list, even when the relation is unsaved
$searchList = DataList::create(TaxonomyTerm::class);
$singleSelectTypeIDs = TaxonomyTerm::getSingleSelectOnlyTypes($this->getOwner()->Tags())->column('ID');
if (!empty($singleSelectTypeIDs)) {
$searchList = $searchList->exclude('TypeID', $singleSelectTypeIDs);
}
// TODO: find-out why the following sorting doesn't effect in the real time.
// $searchList = $searchList->sort(['TypeID'=>'ASC', 'Name' => 'ASC']);
// TaxonomyTerm::config()->update('default_sort', ['TypeID'=>'ASC', 'Name' => 'ASC']);
$addExisting->setSearchList($searchList);

$gridFieldConfig = FieldsProvider::getTaggingGridFieldConfig(
$searchList,
['getAllRequiredTypesNames' => 'Requires']
);

$fields->findOrMakeTab('Root.Tags', _t(self::class . '.TagsTabTitle', 'Tags'));
$fields->addFieldToTab(
Expand All @@ -111,7 +80,7 @@ public function updateCMSFields(FieldList $fields)
'Tags',
_t(self::class . '.ManyManyTags', 'Tags'),
$this->getOwner()->Tags(),
$components
$gridFieldConfig
)
);

Expand Down
72 changes: 72 additions & 0 deletions src/Forms/FieldsProvider.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
<?php

namespace Chrometoaster\AdvancedTaxonomies\Forms;

use Chrometoaster\AdvancedTaxonomies\Models\TaxonomyTerm;
use Chrometoaster\AdvancedTaxonomies\Forms\GridFieldAddTagsAutocompleter;
use Chrometoaster\AdvancedTaxonomies\Forms\GridFieldInfoLink;
use Chrometoaster\AdvancedTaxonomies\Forms\GridFieldOrderableRows;
use SilverStripe\Forms\GridField\GridFieldAddExistingAutocompleter;
use SilverStripe\Forms\GridField\GridFieldAddNewButton;
use SilverStripe\Forms\GridField\GridFieldConfig;
use SilverStripe\Forms\GridField\GridFieldConfig_RelationEditor;
use SilverStripe\Forms\GridField\GridFieldDataColumns;
use SilverStripe\Forms\GridField\GridFieldEditButton;
use SilverStripe\Forms\GridField\GridFieldFilterHeader;
use SilverStripe\ORM\DataList;
use SilverStripe\Versioned\GridFieldArchiveAction;

/**
* Class FieldsProvider
*
* Providing reusable fields and field configurations.
*/
class FieldsProvider
{
/**
* Provide a reusable gridfield config for tagging a dataobject with some terms
*
* @param DataList|null $searchList
* @param array $extraDisplayFields
* @param string $sortField
* @return GridFieldConfig
*/
public static function getTaggingGridFieldConfig(DataList $searchList = null, array $extraDisplayFields = [], string $sortField = 'Sort'): GridFieldConfig
{
// Remove config components from the Tags gridfield to disallow adding/deleting/archiving taxonomy terms from here
$gfc = GridFieldConfig_RelationEditor::create();
$gfc->removeComponentsByType([
GridFieldAddNewButton::class,
GridFieldEditButton::class,
GridFieldArchiveAction::class,
GridFieldFilterHeader::class,
]);

$gfc->getComponentByType(GridFieldDataColumns::class)->setDisplayFields(array_merge(
[
'getNameAsTagWithExtraInfo' => 'Name',
'getDescription15Words' => 'Description',
],
$extraDisplayFields
));

$gfc->addComponent(GridFieldOrderableRows::create($sortField));

// Shift the GridFieldAddExistingAutocompleter component to left
$gfc->removeComponentsByType(GridFieldAddExistingAutocompleter::class);
$gfc->addComponent(
$addExisting = new GridFieldAddTagsAutocompleter('buttons-before-left')
);

$autoResultFormat = '&nbsp;{$getTermHierarchy}&nbsp;';
$addExisting->setResultsFormat($autoResultFormat);
$addExisting->setPlaceholderText('Add tags by name');
$addExisting->setSearchList($searchList ?? TaxonomyTerm::get());

$gfc->addComponent(
new GridFieldInfoLink('buttons-before-left', '/at-taxonomy-overview', "Open 'All taxonomies' overview")
);

return $gfc;
}
}

0 comments on commit 98f2bf8

Please sign in to comment.