Skip to content

Commit

Permalink
Extract tagging gridfield configuration into a provider for reusability
Browse files Browse the repository at this point in the history
  • Loading branch information
michalkleiner committed Mar 16, 2020
1 parent 233b3c1 commit 536a8e8
Show file tree
Hide file tree
Showing 2 changed files with 79 additions and 38 deletions.
48 changes: 10 additions & 38 deletions src/Extensions/DataObjectTaxonomiesDataExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace Chrometoaster\AdvancedTaxonomies\Extensions;

use Chrometoaster\AdvancedTaxonomies\Forms\FieldsProvider;
use Chrometoaster\AdvancedTaxonomies\Forms\GridFieldAddTagsAutocompleter;
use Chrometoaster\AdvancedTaxonomies\Forms\GridFieldInfoLink;
use Chrometoaster\AdvancedTaxonomies\Forms\GridFieldOrderableRows;
Expand Down Expand Up @@ -61,48 +62,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 +83,7 @@ public function updateCMSFields(FieldList $fields)
'Tags',
_t(self::class . '.ManyManyTags', 'Tags'),
$this->getOwner()->Tags(),
$components
$gridFieldConfig
)
);

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

namespace Chrometoaster\AdvancedTaxonomies\Forms;

use Chrometoaster\AdvancedTaxonomies\Models\TaxonomyTerm;
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 536a8e8

Please sign in to comment.