File: D:/HostingSpaces/SBogers10/finsteps.komma.pro/app/Base/SectionWithSiteLogic.php
<?php declare(strict_types=1);
namespace App\Base;
use Komma\KMS\Core\Attributes\Attribute;
use Komma\KMS\Core\Sections\Section as KmsSection;
use Komma\KMS\Core\Sections\Tabs\Collections\AbstractSectionTabs;
use Komma\KMS\Sites\Models\Site;
use Komma\KMS\Sites\SiteServiceInterface;
abstract class SectionWithSiteLogic extends KmsSection
{
/** @var SiteServiceInterface $this->siteService */
protected $siteService;
public function __construct(AbstractSectionTabs $tabs, string $slug)
{
$this->siteService = app(SiteServiceInterface::class);
parent::__construct($tabs, $slug);
}
/**
* This method create copies of the attributes you pass it for all current site languages and
* injects translations via another method
*
* @param $baseAttributes
* @return array
*/
protected function createAttributesFromExistingAttributeForCurrentSiteLanguages(Array $baseAttributes)
{
//Get the languages
/** @var SiteServiceInterface $this->siteService */
$languages = $this->siteService->getSiteLanguages();
$attributes = [];
foreach ($baseAttributes as $baseAttribute) {
$attributesCreatedFromBaseAttribute = $this->createAttributesFromExistingAttributeForTheGivenLanguages($baseAttribute, $languages);
$attributes = array_merge($attributes, $attributesCreatedFromBaseAttribute);
}
return $attributes;
}
/**
* This method create copies of the attributes you pass it for all used site languages and
* injects translations via another method.
*
* @param $baseAttributes
* @return array
*/
protected function createAttributesFromExistingAttributeForAllUsedLanguagesBySites(Array $baseAttributes)
{
$languages = Site::has('languages')->with(['languages'])->get()->map(function (Site $site) {
return $site->languages;
})->collapse();
$currentSiteDefaultLanguage = $this->siteService->getCurrentSite()->defaultLanguage;
$languages = $languages->push($currentSiteDefaultLanguage)->unique('id');
$attributes = [];
foreach ($baseAttributes as $baseAttribute) {
/** @var Attribute $baseAttribute */
$attributesCreatedFromBaseAttribute = $this->createAttributesFromExistingAttributeForTheGivenLanguages($baseAttribute,
$languages);
// if($baseAttribute->getsValueFrom() == Attribute::ValueFromItself && $baseAttribute->getsValueFromReference() == 'value') dd($attributesCreatedFromBaseAttribute); //Debugging helper
$attributes = array_merge($attributes, $attributesCreatedFromBaseAttribute);
}
return $attributes;
}
}