Skip to content

Commit

Permalink
Merge pull request #24 from wrav/v2
Browse files Browse the repository at this point in the history
feat: Add scope to all sites or a selected site
  • Loading branch information
reganlawton authored Sep 15, 2023
2 parents 5f5456f + 55324b7 commit fd2d194
Show file tree
Hide file tree
Showing 6 changed files with 46 additions and 7 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
# Related Changelog

## 2.1.1 - 2023-09-15

### Updated
- Add scope to all sites or a selected site, thanks @OscarBarrett for the issue #20

## 2.1.0 - 2023-09-15

### Updated
Expand Down
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"name": "wrav/related",
"description": "A simple plugin that adds a widget within the Craft CP page sidebar, allowing you to quickly and easily access related entries.",
"type": "craft-plugin",
"version": "2.1.0",
"version": "2.1.1",
"keywords": [
"craft",
"cms",
Expand Down
14 changes: 14 additions & 0 deletions src/Related.php
Original file line number Diff line number Diff line change
Expand Up @@ -137,13 +137,26 @@ protected function createSettingsModel(): ?craft\base\Model

protected function settingsHtml(): string
{
// Fetch the available sites
$sites = Craft::$app->sites->getAllSites();

// Fetch other data you need (sections, categories, asset volumes)
$sections = Craft::$app->getSections()->getAllSections();
$categories = Craft::$app->getCategories()->getAllGroups();
$assetVolumes = Craft::$app->getVolumes()->getAllVolumes();

// Prepare options arrays
$optionsSections = [];
$optionsCategories = [];
$optionsAssetVolumes = [];
$siteOptions = [];

$siteOptions['*'] = 'All Sites';

foreach ($sites as $site) {
// Populate site options with site ID as the value and site name as the label
$siteOptions[$site->id] = $site->name;
}

foreach ($sections as $id => $section) {
$optionsSections[$section->handle] = $section->name;
Expand All @@ -167,6 +180,7 @@ protected function settingsHtml(): string
'optionsSections' => $optionsSections,
'optionsCategories' => $optionsCategories,
'optionsAssetVolumes' => $optionsAssetVolumes,
'siteOptions' => $siteOptions,
]
);
}
Expand Down
5 changes: 5 additions & 0 deletions src/models/Settings.php
Original file line number Diff line number Diff line change
Expand Up @@ -46,4 +46,9 @@ class Settings extends Model
* @var string
*/
public $allowedAssetVolumes;

/**
* @var string
*/
public $selectedSite;
}
18 changes: 12 additions & 6 deletions src/services/RelatedService.php
Original file line number Diff line number Diff line change
Expand Up @@ -47,12 +47,18 @@ public function getRelated($elementId)
return [];
}

$selectedSite = Related::getInstance()->getSettings()->selectedSite ?? '*';

if ('*' !== $selectedSite) {
$selectedSite = Craft::$app->sites->getSiteById($selectedSite);
}

/** @var Element $element */
$element = Craft::$app->elements->getElementById($elementId);
/** @var Element $elementType */
$elementType = Craft::$app->elements->getElementTypeById($elementId);

$query = MatrixBlock::find();
$query = MatrixBlock::find()->site($selectedSite);
$query->relatedTo = $element;
$query->anyStatus();
/** @var MatrixBlock[] $blocks */
Expand All @@ -67,28 +73,28 @@ public function getRelated($elementId)
}

/** @var Query $query */
$query = Entry::find();
$query = Entry::find()->site($selectedSite);
$query->relatedTo = $element;
$query->anyStatus();
/** @var Element[] $entries */
$entries = $query->all();

/** @var Query $query */
$query = Category::find();
$query = Category::find()->site($selectedSite);
$query->relatedTo = $element;
$query->anyStatus();
/** @var Element[] $categories */
$categories = $query->all();

/** @var Query $query */
$query = User::find();
$query = User::find()->site($selectedSite);
$query->relatedTo = $element;
$query->anyStatus();
/** @var Element[] $users */
$users = $query->all();

/** @var Query $query */
$query = User::find();
$query = User::find()->site($selectedSite);
$query->relatedTo = $element;
$query->anyStatus();
/** @var Element[] $users */
Expand All @@ -97,7 +103,7 @@ public function getRelated($elementId)
$products = [];
try {
if (class_exists('craft\commerce\elements\Product')) {
$query = craft\commerce\elements\Product::find();
$query = craft\commerce\elements\Product::find()->site($selectedSite);
$query->relatedTo = $element;
$query->anyStatus();
$products = $query->all();
Expand Down
9 changes: 9 additions & 0 deletions src/templates/settings.twig
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,15 @@

{% import "_includes/forms" as forms %}

{{ forms.selectField({
label: "Select Site"|t,
instructions: "Choose a site"|t,
id: 'selectedSite',
name: 'selectedSite',
options: siteOptions,
value: settings['selectedSite'] ?? null,
}) }}

{{ forms.multiselectField({
label: "Allowed Sections"|t,
instructions: "Choose which sections the relation widget will show on. Leave blank for all sections"|t,
Expand Down

0 comments on commit fd2d194

Please sign in to comment.