acf-builder icon indicating copy to clipboard operation
acf-builder copied to clipboard

Feature Request: Bidirectional fields

Open mortensassi opened this issue 4 years ago • 1 comments

When i used ACF tohether ACF-Extended i really enjoyed having the "Bidirectional Field" Option on relatinship fields - Is it possible to implement such functionality to this plugin? Would love to hear your thoughts. (And sorry for being so ignorant and not submitting my own solution)

mortensassi avatar Jul 14 '20 16:07 mortensassi

You can do this when you set up addRelationship.

class AttachmentProject extends Partial
{
    public function fields()
    {
        $attachmentProject = new FieldsBuilder('attachment_project');

        $attachmentProject
            ->addRelationship('project', [
                'post_type' => 'project',
                'acfe_bidirectional' => [
                    'acfe_bidirectional_enabled' => true,
                    'acfe_bidirectional_related' => [
                        'field_project_attachment',
                    ],
                ],
            ]);

        return $attachmentProject;
    }
}
class ProjectAttachment extends Partial
{
    public function fields()
    {
        $projectAttachment = new FieldsBuilder('project_attachment');

        $projectAttachment
            ->addRelationship('attachment', [
                'post_type' => 'attachment',
                'acfe_bidirectional' => array(
                    'acfe_bidirectional_enabled' => true,
                    'acfe_bidirectional_related' => [
                        'field_attachment_project',
                    ],
                ),
            ]);

        return $projectAttachment;
    }
}

When referencing the other field to link, the name is field_ plus whatever you've named the other field's FieldsBuilder instance (not sure why it takes that name rather than the relationship field name). Or you can get the field name from looking at the admin area in the web inspector.

robrecord avatar May 19 '22 11:05 robrecord