Skip to content

Commit

Permalink
Add logic to select section by part of handle
Browse files Browse the repository at this point in the history
  • Loading branch information
wmdhosting authored May 12, 2023
1 parent b95b55b commit bc120ea
Showing 1 changed file with 36 additions and 1 deletion.
37 changes: 36 additions & 1 deletion src/fields/SectionField.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,11 @@ class SectionField extends Field implements PreviewableFieldInterface
*/
public array $excludedSections = [];

/**
* @var string Part of handle for selected section by this part
*/
public string $partOfHandle = '';

/**
* @inheritdoc
*/
Expand Down Expand Up @@ -89,6 +94,25 @@ private function getSections()

return $sections;
}

/**
* Return all sections handles.
*
* @return array
*/
private function getSectionsHandles()
{
$sections = [];
$editableSections = Craft::$app->getSections()->getEditableSections();

if (!empty($editableSections)) {
foreach ($editableSections as $section) {
$sections[$section->id] = $section->handle;
}
}

return $sections;
}

/**
* @inheritdoc
Expand Down Expand Up @@ -178,6 +202,7 @@ public function getSettingsHtml(): ?string
'field' => $this,
'sections' => $this->getSections(),
'selectAll' => $this->selectAll,
'partOfHandle' => $this->partOfHandle,
]
);
}
Expand All @@ -187,7 +212,9 @@ public function getSettingsHtml(): ?string
*/
public function getInputHtml($value, ElementInterface $element = null): string
{
if (empty($this->allowedSections) && empty($this->selectAll)) return 'You have not selected any section for selection, select in the field settings.';
if (empty($this->allowedSections) && empty($this->selectAll) && empty($this->partOfHandle)) {
return 'You have not selected any section for selection, select in the field settings.';
}

$sections = $this->getSections();
$allowSectionsConfig = $this->allowedSections;
Expand All @@ -199,6 +226,14 @@ public function getInputHtml($value, ElementInterface $element = null): string
}
}
$allowSectionsConfig = array_keys($sections);
} else if(!empty($this->partOfHandle)) {
foreach ($this->getSectionsHandles() as $id => $handle) {
if (stripos($handle, $this->partOfHandle) !== false
&& !in_array($id, $allowSectionsConfig)
&& !in_array($id, $this->excludedSections)) {
$allowSectionsConfig[] = $id;
}
}
}

$allowSections = array_flip($allowSectionsConfig);
Expand Down

0 comments on commit bc120ea

Please sign in to comment.