Skip to content
Permalink
Branch: master
Find file Copy path
Find file Copy path
Fetching contributors…
Cannot retrieve contributors at this time
196 lines (164 sloc) 4.78 KB
<?php
/**
* @link https://craftcms.com/
* @copyright Copyright (c) Pixel & Tonic, Inc.
* @license https://craftcms.github.io/license/
*/
namespace craft\fields;
use Craft;
use craft\base\ElementInterface;
use craft\db\Table as DbTable;
use craft\elements\Category;
use craft\elements\db\CategoryQuery;
use craft\gql\arguments\elements\Category as CategoryArguments;
use craft\gql\interfaces\elements\Category as CategoryInterface;
use craft\gql\resolvers\elements\Category as CategoryResolver;
use craft\helpers\ArrayHelper;
use craft\helpers\Db;
use craft\helpers\ElementHelper;
use craft\helpers\Gql;
use GraphQL\Type\Definition\Type;
/**
* Categories represents a Categories field.
*
* @author Pixel & Tonic, Inc. <support@pixelandtonic.com>
* @since 3.0.0
*/
class Categories extends BaseRelationField
{
// Static
// =========================================================================
/**
* @inheritdoc
*/
public static function displayName(): string
{
return Craft::t('app', 'Categories');
}
/**
* @inheritdoc
*/
protected static function elementType(): string
{
return Category::class;
}
/**
* @inheritdoc
*/
public static function defaultSelectionLabel(): string
{
return Craft::t('app', 'Add a category');
}
/**
* @inheritdoc
*/
public static function valueType(): string
{
return CategoryQuery::class;
}
// Properties
// =========================================================================
/**
* @inheritdoc
*/
public $allowLimit = false;
/**
* @inheritdoc
*/
public $allowMultipleSources = false;
/**
* @var int|null Branch limit
*/
public $branchLimit;
/**
* @inheritdoc
*/
protected $settingsTemplate = '_components/fieldtypes/Categories/settings';
/**
* @inheritdoc
*/
protected $inputTemplate = '_components/fieldtypes/Categories/input';
/**
* @inheritdoc
*/
protected $inputJsClass = 'Craft.CategorySelectInput';
/**
* @inheritdoc
*/
protected $sortable = false;
// Public Methods
// =========================================================================
/**
* @inheritdoc
*/
public function normalizeValue($value, ElementInterface $element = null)
{
if (is_array($value)) {
/** @var Category[] $categories */
$categories = Category::find()
->siteId($this->targetSiteId($element))
->id(array_values(array_filter($value)))
->anyStatus()
->all();
// Fill in any gaps
$categoriesService = Craft::$app->getCategories();
$categoriesService->fillGapsInCategories($categories);
// Enforce the branch limit
if ($this->branchLimit) {
$categoriesService->applyBranchLimitToCategories($categories, $this->branchLimit);
}
$value = ArrayHelper::getColumn($categories, 'id');
}
return parent::normalizeValue($value, $element);
}
/**
* @inheritdoc
*/
public function getInputHtml($value, ElementInterface $element = null): string
{
// Make sure the field is set to a valid category group
if ($this->source) {
$source = ElementHelper::findSource(static::elementType(), $this->source, 'field');
}
if (empty($source)) {
return '<p class="error">' . Craft::t('app', 'This field is not set to a valid category group.') . '</p>';
}
return parent::getInputHtml($value, $element);
}
/**
* @inheritdoc
*/
protected function inputTemplateVariables($value = null, ElementInterface $element = null): array
{
$variables = parent::inputTemplateVariables($value, $element);
$variables['branchLimit'] = $this->branchLimit;
return $variables;
}
/**
* @inheritdoc
* @since 3.3.0
*/
public function getContentGqlType()
{
return [
'name' => $this->handle,
'type' => Type::listOf(CategoryInterface::getType()),
'args' => CategoryArguments::getArguments(),
'resolve' => CategoryResolver::class . '::resolve',
];
}
/**
* @inheritdoc
* @since 3.3.0
*/
public function getEagerLoadingGqlConditions()
{
$allowedEntities = Gql::extractAllowedEntitiesFromSchema();
$allowedCategoryUids = $allowedEntities['categorygroups'] ?? [];
if (empty($allowedCategoryUids)) {
return false;
}
$categoryIds = Db::idsByUids(DbTable::CATEGORYGROUPS, $allowedCategoryUids);
return ['groupId' => array_values($categoryIds)];
}
}
You can’t perform that action at this time.