Skip to content

Commit

Permalink
## 1.0.3 - 2021-10-25
Browse files Browse the repository at this point in the history
### Fixed
- Removed translation methods in field edit view..
  • Loading branch information
internetztube committed Oct 25, 2021
1 parent 9e08fc8 commit 7a91ebd
Show file tree
Hide file tree
Showing 5 changed files with 94 additions and 12 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,10 @@ All notable changes to this project will be documented in this file.

The format is based on [Keep a Changelog](http://keepachangelog.com/) and this project adheres to [Semantic Versioning](http://semver.org/).

## 1.0.3 - 2021-10-25
### Added
- Added support for SEOmatic.

## 1.0.3 - 2021-10-25
### Fixed
- Removed translation methods in field edit view..
Expand Down
11 changes: 11 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,17 @@ To install this plugin, follow these steps:

You can also install this plugin via the Plugin Store in the Craft Control Panel.

## Support
As a basis the relations table is used. This means that any field that stores relations in the relations table will work out of the box.
* Most Craft CMS internal fields
* NEO
* SuperTable
* SEOmatic
* ... and many more.

**Not supported:**
* Redactor

## Screenshots
![Field Edit Page](screenshots/field.jpg)
Create Field
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": "internetztube/craft-element-relations",
"description": "Shows all relations of an element.",
"type": "craft-plugin",
"version": "1.0.3",
"version": "1.0.4",
"keywords": [
"craft",
"cms",
Expand Down
27 changes: 19 additions & 8 deletions src/controllers/ElementRelationsController.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,17 +29,28 @@ public function actionGetByElementId()
$element = \Craft::$app->elements->getElementById($elementId, null, $siteId);
if (!$element) throw new NotFoundHttpException;
$relations = ElementRelationsService::getRelationsFromElement($element);
$isUsedInSEOmatic = ElementRelationsService::isUsedInSEOmatic($element);

if (!count($relations)) {
$result = collect();

if ($isUsedInSEOmatic['usedGlobally']) {
$result->push('Used in SEOmatic Global Settings');
}
if (!empty($isUsedInSEOmatic['elements'])) {
$result->push('Used in SEOmatic in these Elements (+Drafts):');
$result->push(Cp::elementPreviewHtml($isUsedInSEOmatic['elements'], $size));
}
if (!empty($relations)) {
$result->push(Cp::elementPreviewHtml($relations, $size));
} else {
$relationsAnySite = ElementRelationsService::getRelationsFromElement($element, true);
if (count($relationsAnySite) === 0) {
return '<span style="color: #da5a47;">Unused</span>';
} else {
$result = 'Unused in this site, but used in others:<br />';
$result .= Cp::elementPreviewHtml($relationsAnySite, $size);
return $result;
if (!empty($relationsAnySite)) {
$result->push('Unused in this site, but used in others:');
$result->push(Cp::elementPreviewHtml($relationsAnySite, $size));
}
}
return Cp::elementPreviewHtml($relations, $size);

if ($result->isEmpty()) { $result->push('<span style="color: #da5a47;">Unused</span>'); }
return $result->implode('<br />');
}
}
62 changes: 59 additions & 3 deletions src/services/ElementRelationsService.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,6 @@
use craft\base\Element;
use craft\db\Query;
use craft\db\Table;
use craft\elements\Entry;
use craft\models\Site;
use yii\base\BaseObject;

class ElementRelationsService extends Component
{
Expand All @@ -23,6 +20,7 @@ public static function getRelationsFromElement(Element $sourceElement, bool $any
$site = $anySite ? '*' : $sourceElement->site;

return collect($elements)->map(function(int $elementId) use ($site) {
/** @var ?Element $relation */
$relation = self::getElementById($elementId, $site);
if (!$relation) { return null; }
return self::getRootElement($relation, $site);
Expand All @@ -31,11 +29,69 @@ public static function getRelationsFromElement(Element $sourceElement, bool $any

private static function getElementById (int $elementId, $site): ?Element
{
if (is_numeric($site)) {
$site = \Craft::$app->sites->getSiteById($site);
}
$result = (new Query())->select(['type'])->from(Table::ELEMENTS)->where(['id' => $elementId])->one();
if (!$result) { return null; } // relation is broken
return $result['type']::find()->id($elementId)->anyStatus()->site($site)->one();
}

public static function isUsedInSEOmatic(Element $sourceElement)
{
$result = ['usedGlobally' => false, 'elements' => []];
$isInstalled = \Craft::$app->db->tableExists('{{%seomatic_metabundles}}');
if (!$isInstalled) { return false; }

$extractIdFromString = function ($input) {
if (!$input) { return; }
$result = sscanf($input, '{seomatic.helper.socialTransform(%d, ');
return (int) collect($result)->first();
};

$globalQueryResult = (new Query)->select(['metaGlobalVars', 'metaSiteVars'])
->from('{{%seomatic_metabundles}}')
->all();

$result['usedGlobally'] = collect($globalQueryResult)
->map(function($row) { return collect($row)->values(); })
->flatten()
->map(function($row) { return json_decode($row, true); })
->map(function ($row) use ($extractIdFromString) {
if (isset($row['seoImage'])) { return $extractIdFromString($row['seoImage']); }
if (isset($row['identity']['genericImageIds'])) { return $row['identity']['genericImageIds']; }
return null;
})
->flatten()->filter()
->map(function($row) { return (int) $row; })->unique()
->contains($sourceElement->id);

$fields = (new Query)->select(['handle'])
->from(Table::FIELDS)
->where(['=', 'type', 'nystudio107\seomatic\fields\SeoSettings'])
->column();

$foundElements = collect();

collect($fields)->each(function ($handle) use (&$foundElements, $extractIdFromString, $sourceElement) {
$fieldHandle = sprintf('field_%s', $handle);
$rows = (new Query)->select(['elements.canonicalId', 'elements.id', 'siteId', 'title', 'content.'.$fieldHandle])
->from(Table::CONTENT)
->innerJoin(Table::ELEMENTS, '[[elements.id]] = [[content.elementId]]')
->where(['NOT', ['content.'.$fieldHandle => null]])
->all();
collect($rows)->each(function ($row) use (&$foundElements, $extractIdFromString, $fieldHandle, $sourceElement) {
$data = json_decode($row[$fieldHandle]);
$id = $extractIdFromString($data->metaGlobalVars->seoImage);
if ($id !== $sourceElement->id) { return; }
$foundElements->push(self::getElementById($row['canonicalId'] ?? $row['id'], $row['siteId']));
});
});
$result['elements'] = collect($foundElements)
->unique('canonialId')
->toArray();
return $result;
}
private static function getRootElement (Element $element, $site): ?Element
{
if (!isset($element->ownerId) || !$element->ownerId) { return $element; }
Expand Down

0 comments on commit 7a91ebd

Please sign in to comment.