Is it Possible to Have Relationship Metabox Editable Only on One Side?

Support MB Relationships Is it Possible to Have Relationship Metabox Editable Only on One Side?

Viewing 8 posts - 1 through 8 (of 8 total)
  • Author
    Posts
  • #34602
    petr@blackeye.cz[email protected]
    Participant

    Hello,

    Is it possible to have a relationship field editable only on one side of the relation? Lets say I have a list of Courses and the list of Applications. I want to select one Course for each Application and I want to see the list of the Applications in the detail of the Course. But I don't want to have a possibility to change anything in the list of the Applications inside the Course detail.

    So far, I was able to limit the number of Courses selectable in the Application detail, but I'm not able to disable editing of the Applications in the Course detail.

    Is it possible, for example using some class?

    Thanks
    Petr

    #34687
    Long NguyenLong Nguyen
    Moderator

    Hi,

    Yes, it is possible. You can use the setting reciprocal when registering the relationship. If you set reciprocal for different object types (CPT) like your case, the connection meta box only shows on the from object type page.

    Refer to the documentation https://docs.metabox.io/extensions/mb-relationships/#reciprocal-relationships

    #34689
    petr@blackeye.cz[email protected]
    Participant

    Hi Long,

    I must be very bad at explaining my questions... I clearly stated, that I want to show the metabox in both CPTs. I only want one of them to be disabled for editing. Again, I want to SEE them both, but I don't want to EDIT one of them.

    Thanks again
    Petr

    #35182
    Long NguyenLong Nguyen
    Moderator

    Hi,

    You can use the field type select and add the attribute disabled to an object type to disable edit/add more connected objects. For example:

    MB_Relationships_API::register( [
        'id'   => 'job-to-post',
        'from' => [
            'object_type'  => 'post',
            'post_type'    => 'job',
            'field' => [
                'type' => 'select',
                'disabled' => true //here
            ]    
        ],
        'to'   => [
            'object_type'  => 'post',
            'post_type'    => 'post',
            
        ],
    ] );

    Read more on the documentation https://docs.metabox.io/extensions/mb-relationships/#syntax
    https://docs.metabox.io/field-settings/

    #35185
    petr@blackeye.cz[email protected]
    Participant

    Hello Long,

    Thank you, I'll will try it, but first I have to say, that I'm usig GUI for creating the relationship. Since there's (probably) not a field for this parameter in there, how should I update my relationship settings? Or do I have to register this relationship only by code?

    Thanks
    Petr

    #35206
    Long NguyenLong Nguyen
    Moderator

    Hi,

    The builder does not support options to customize the field settings. You need to use the code to get more control over field settings.

    #35213
    petr@blackeye.cz[email protected]
    Participant

    Hi Long,

    OK, I will use the code, but HOW should I do this? Do I need to remove the orginal relationship from the GUI and start from the scratch or can I use some code to change only this option separately?

    Thanks
    Petr

    #35231
    Long NguyenLong Nguyen
    Moderator

    Hi,

    You can export the relationship from the builder to get the PHP code, move it to the trash or delete it. Then add the code to the file functions.php in the theme/child theme folder.

    The data is already saved into the database so don't worry it will be lost after deleting the relationship from the builder.

    Refer to this documentation https://docs.metabox.io/extensions/meta-box-builder/#getting-php-code

Viewing 8 posts - 1 through 8 (of 8 total)
  • You must be logged in to reply to this topic.