Skip to content

Commit

Permalink
MNT: Simplify field formatting
Browse files Browse the repository at this point in the history
  • Loading branch information
Cambis committed Aug 12, 2024
1 parent 81a1cd8 commit aeaf9cf
Showing 1 changed file with 16 additions and 66 deletions.
82 changes: 16 additions & 66 deletions src/Admin/AdvancedWorkflowAdmin.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@

use InvalidArgumentException;
use SilverStripe\Admin\ModelAdmin;
use SilverStripe\CMS\Controllers\CMSPageEditController;
use SilverStripe\Control\HTTPRequest;
use SilverStripe\Control\HTTPResponse;
use SilverStripe\Forms\FieldList;
Expand Down Expand Up @@ -111,8 +110,13 @@ public function getEditForm($id = null, $fields = null)
$config->addComponent(GridFieldEditButton::create());
$config->addComponent(GridFieldDetailForm::create());
$config->getComponentByType(GridFieldPaginator::class)->setItemsPerPage(5);

$columns = $config->getComponentByType(GridFieldDataColumns::class);
$columns->setFieldFormatting($this->setFieldFormatting($config));

if ($columns instanceof GridFieldDataColumns) {
$columns->setDisplayFields($this->getItemDisplayFields())
->setFieldFormatting($this->getItemFieldFormatting());
}

// Show items submitted into a workflow for current user to action
$pending = $this->getPendingItems($config);
Expand Down Expand Up @@ -165,50 +169,6 @@ public function isAdminUser(Member $user)
return false;
}

/*
* By default, we implement GridField_ColumnProvider to allow users to click through to the PagesAdmin.
* We would also like a "Quick View", that allows users to quickly make a decision on a given workflow-bound
* content-object
*/
public function columns()
{
$fields = array(
'Title' => array(
'link' => function ($value, $item) {
$pageAdminLink = singleton(CMSPageEditController::class)->Link('show');
return sprintf('<a href="%s/%s">%s</a>', $pageAdminLink, $item->Link, $value);
}
),
'WorkflowStatus' => array(
'text' => function ($value, $item) {
return $item->WorkflowCurrentAction;
}
)
);
return $fields;
}

/*
* Discreet method used by both intro gridfields to format the target object's links and clickable text
*
* @param GridFieldConfig $config
* @return array $fieldFormatting
*/
public function setFieldFormatting(&$config)
{
$fieldFormatting = array();
// Parse the column information
foreach ($this->columns() as $source => $info) {
if (isset($info['link']) && $info['link']) {
$fieldFormatting[$source] = '<a href=\"$ObjectRecordLink\">$value</a>';
}
if (isset($info['text']) && $info['text']) {
$fieldFormatting[$source] = $info['text'];
}
}
return $fieldFormatting;
}

/**
* Return content-object data depending on which gridfeld is calling for it
*
Expand Down Expand Up @@ -330,7 +290,7 @@ public function getWorkflowService()
*/
private function getItemDisplayFields(): array
{
if ($this->config()->get('fieldOverrides')) {
if ($this->config()->get('fieldOverrides') !== []) {
return $this->config()->get('fieldOverrides');
}

Expand All @@ -343,6 +303,10 @@ private function getItemDisplayFields(): array
}

/**
* By default, we implement GridField_ColumnProvider to allow users to click through to the PagesAdmin.
* We would also like a "Quick View", that allows users to quickly make a decision on a given workflow-bound
* content-object
*
* @return string[]
*/
private function getItemFieldFormatting(): array
Expand All @@ -355,7 +319,11 @@ private function getItemFieldFormatting(): array
return $value;
}

return $target->Title;
if (!$target->hasMethod('CMSEditLink')) {
return $value;
}

return sprintf('<a href="%s">%s</a>', $target->CMSEditLink(), $target->Title);
},
'LastEdited' => static function (string $value, WorkflowInstance $instance): string {
$target = $instance->getTarget();
Expand Down Expand Up @@ -412,15 +380,6 @@ private function getPendingItems(GridFieldConfig $config): ?GridField
$config
);

$dataColumns = $gridField->getConfig()->getComponentByType(GridFieldDataColumns::class);

if (!$dataColumns instanceof GridFieldDataColumns) {
return $gridField;
}

$dataColumns->setDisplayFields($this->getItemDisplayFields())
->setFieldFormatting($this->getItemFieldFormatting());

return $gridField;
}

Expand Down Expand Up @@ -454,15 +413,6 @@ private function getSubmittedItems(GridFieldConfig $config): ?GridField
$gridField->getConfig()->removeComponentsByType(GridFieldEditButton::class)
->addComponent(GridFieldWorkflowRestrictedEditButton::create());

$dataColumns = $gridField->getConfig()->getComponentByType(GridFieldDataColumns::class);

if (!$dataColumns instanceof GridFieldDataColumns) {
return $gridField;
}

$dataColumns->setDisplayFields($this->getItemDisplayFields())
->setFieldFormatting($this->getItemFieldFormatting());

return $gridField;
}
}

0 comments on commit aeaf9cf

Please sign in to comment.