-
Notifications
You must be signed in to change notification settings - Fork 823
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
ENH Allow overriding GridFieldFilterHeader placeholder #11418
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -49,6 +49,8 @@ class GridFieldFilterHeader extends AbstractGridFieldComponent implements GridFi | |
*/ | ||
protected ?string $searchField = null; | ||
|
||
private string $placeHolderText = ''; | ||
|
||
/** | ||
* @inheritDoc | ||
*/ | ||
|
@@ -245,6 +247,24 @@ public function canFilterAnyColumns($gridField) | |
return false; | ||
} | ||
|
||
/** | ||
* Get the text to be used as a placeholder in the search field. | ||
* If blank, the placeholder will be generated based on the class held in the GridField. | ||
*/ | ||
public function getPlaceHolderText(): string | ||
{ | ||
return $this->placeHolderText; | ||
} | ||
|
||
/** | ||
* Set the text to be used as a placeholder in the search field. | ||
* If blank, this text will be generated based on the class held in the GridField. | ||
*/ | ||
public function setPlaceHolderText(string $placeHolderText): static | ||
{ | ||
$this->placeHolderText = $placeHolderText; | ||
return $this; | ||
} | ||
|
||
/** | ||
* Generate a search context based on the model class of the of the GridField | ||
|
@@ -318,7 +338,7 @@ public function getSearchFieldSchema(GridField $gridField) | |
$schema = [ | ||
'formSchemaUrl' => $schemaUrl, | ||
'name' => $searchField, | ||
'placeholder' => _t(__CLASS__ . '.Search', 'Search "{name}"', ['name' => $this->getTitle($gridField, $inst)]), | ||
'placeholder' => $this->getPlaceHolder($inst), | ||
'filters' => $filters ?: new \stdClass, // stdClass maps to empty json object '{}' | ||
'gridfield' => $gridField->getName(), | ||
'searchAction' => $searchAction->getAttribute('name'), | ||
|
@@ -330,19 +350,6 @@ public function getSearchFieldSchema(GridField $gridField) | |
return json_encode($schema); | ||
} | ||
|
||
private function getTitle(GridField $gridField, object $inst): string | ||
{ | ||
if ($gridField->Title) { | ||
return $gridField->Title; | ||
} | ||
|
||
if (ClassInfo::hasMethod($inst, 'i18n_plural_name')) { | ||
return $inst->i18n_plural_name(); | ||
} | ||
|
||
return ClassInfo::shortName($inst); | ||
} | ||
|
||
/** | ||
* Returns the search form for the component | ||
* | ||
|
@@ -386,7 +393,7 @@ public function getSearchForm(GridField $gridField) | |
$field->addExtraClass('stacked no-change-track'); | ||
} | ||
|
||
$name = $this->getTitle($gridField, singleton($gridField->getModelClass())); | ||
$name = $this->getTitle(singleton($gridField->getModelClass())); | ||
|
||
$this->searchForm = $form = new Form( | ||
$gridField, | ||
|
@@ -456,4 +463,32 @@ public function getHTMLFragments($gridField) | |
) | ||
]; | ||
} | ||
|
||
/** | ||
* Get the text that will be used as a placeholder in the search field. | ||
* | ||
* @param object $obj An instance of the class that will be searched against. | ||
* If getPlaceHolderText is empty, this object will be used to build the placeholder | ||
* e.g. 'Search "My Data Object"' | ||
*/ | ||
private function getPlaceHolder(object $obj): string | ||
{ | ||
$placeholder = $this->getPlaceHolderText(); | ||
if (!empty($placeholder)) { | ||
return $placeholder; | ||
} | ||
if ($obj) { | ||
return _t(__CLASS__ . '.Search', 'Search "{name}"', ['name' => $this->getTitle($obj)]); | ||
} | ||
return _t(__CLASS__ . '.Search_Default', 'Search'); | ||
} | ||
|
||
private function getTitle(object $inst): string | ||
{ | ||
if (ClassInfo::hasMethod($inst, 'i18n_plural_name')) { | ||
return $inst->i18n_plural_name(); | ||
} | ||
|
||
return ClassInfo::shortName($inst); | ||
} | ||
Comment on lines
+486
to
+493
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Note I've removed the code that relied on See #11416 (comment) for why I did that instead of changing it to use the |
||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Moved this method because private methods should be nearer the bottom than public methods.