Skip to content

Relation field improvements#15400

Merged
brandonkelly merged 7 commits into5.3from
feature/cms-1318-relation-field-improvements
Jul 23, 2024
Merged

Relation field improvements#15400
brandonkelly merged 7 commits into5.3from
feature/cms-1318-relation-field-improvements

Conversation

@brandonkelly
Copy link
Member

@brandonkelly brandonkelly commented Jul 22, 2024

Description

Several improvements for relations in Craft:

  • Relation fields are now multi-instance.
  • Relation fields now get all the normal Translation Method options. (Existing fields will be set to either “Not translatable” or “Translate for each site”, depending on their previous “Manage relations on a per-site basis” setting.)
  • Link fields now store relations when an element is selected—so they now get factored in by relatedTo element query params—and the framework is in place for other field types (e.g. CKEditor) to start storing relations as well.

Field types which should store relations (in addition to their normal field data) must implement RelationalFieldInterface, and optionally import RelationalFieldTrait.

use craft\base\Field;
use craft\base\RelationalFieldInterface;
use craft\base\RelationalFieldTrait;

class MyFieldType extends Field implements RelationalFieldInterface
{
    use RelationalFieldTrait;

    public function getRelationTargetIds(ElementInterface $element): array
    {
        $value = $element->getFieldValue($this->handle);
        return $this->scrapeRelationIdsFromValue($value);
    }

    private function scrapeRelationIdsFromValue(mixed $value): array
    {
        // ...
    }
}

Related issues

@linear
Copy link

linear bot commented Jul 22, 2024

@brandonkelly brandonkelly changed the title Feature/cms 1318 relation field improvements Relation field improvements Jul 22, 2024
@brandonkelly brandonkelly merged commit 44fe4e9 into 5.3 Jul 23, 2024
@brandonkelly brandonkelly deleted the feature/cms-1318-relation-field-improvements branch July 23, 2024 13:34
@darylknight
Copy link

What does "multi-instance" mean?

@bencroker
Copy link
Contributor

It means that you can create multiple instances of the same field.

@brandonkelly
Copy link
Member Author

Right… now you can drag the same relation field into a field layout multiple times (like you already could with most other field types in Craft 5).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants