0

I would like to have a Rent/Sell radio button for 3 content types. Then I want to show this radio buttons on a views exposed filter. How is this possible to do without sharing the radio buttons field on the 3 content types i.e. use a different field on each content type (so as to make easier to upgrade to D8).

0

1 Answer 1

1

If you add all 3 fields as exposed filters and hide 2 of them (with CSS or if you set all fields as "hidden" type with hook_form_FORM_ID_alter()). Then you can use the visible field as master and alter values of the two others with a #submit callback.

<?php
/**
 * Implementation of hook_form_FORM_ID_alter().
 */
function mymodule_form_views_exposed_form_alter(&$form, &$form_state) {
    if (isset($form['field_to_expose1'])) {
        // Hide two fields and left one exposed.
        $form['field_to_hide2']['#type'] = 'hidden';
        $form['field_to_hide3']['#type'] = 'hidden';

        // Execute set_value_for_hidden_fields() as first function submit on $form['#submit'] array.
        array_unshift($form['#submit'], 'set_value_for_hidden_fields');
    }
}

function set_value_for_hidden_fields($form, &$form_state) {
  $value = $form_state['values']['field_to_expose1'];

  // Modify form_state values of concerned field.
  $form_state['values']['field_to_hide2'] = $value;
  $form_state['values']['field_to_hide3'] = $value;

  // Modify form_state of concerned field to pass to the view.
  $form_state['view']->display_handler->handlers['filter']['field_to_hide2']->validated_exposed_input = $value;
  $form_state['view']->display_handler->handlers['filter']['field_to_hide3']->validated_exposed_input = $value;
}

That's unstested code, but I hope you get the idea.

But is it worth the effort? Is sharing a field between multiple content types really a blocking issue when planning an upgrade? I'm not well informed on upgrade procedures for D7 to 8, but sharing a field between content types seems to be a pretty common thing using D7.

3
  • Thank you!As far as I have read,D8 won't support sharing a field between content types.So this would make the upgrade difficult.Has anybody more info on this?
    – Dion
    Jan 10, 2015 at 18:01
  • Or am I wrong?I just found this:"A field cannot be "shared" across entity types, only across bundles of a given entity type" Does this mean that fields can be shared also on D8 between content types?
    – Dion
    Jan 10, 2015 at 18:08
  • So you won't be able to share a field between a node, user or term, not blocking for you :). Jan 10, 2015 at 20:17

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.