Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Relation field improvements #15400

Merged
merged 7 commits into from
Jul 23, 2024

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

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