You are here

function cer_ief_field_widget_form_alter in Corresponding Entity References 7.3

Implements hook_field_widget_form_alter().

@todo This could be cached into an array and only rebuilt when a contextual field instance is updated or CER pattern added/removed.

File

extensions/cer_ief/cer_ief.module, line 34
Provides an option to hide CER fields on inline entity forms. Spun off from Issue #2240371.

Code

function cer_ief_field_widget_form_alter(array &$element, array &$form_state, array $context) {

  // If this widget form isn't in an IEF, bail out.
  if (!isset($context['form']['#ief_id'])) {
    return;
  }

  // Get the form ID.
  $ief_id = $context['form']['#ief_id'];

  // Get instance information for the IEF field.
  $ief_field_instance = $form_state['inline_entity_form'][$ief_id]['instance'];

  // The left chain is the field that *has* the IEF, and the right chain is the
  // field *in* the IEF.
  $left_chain = $ief_field_instance['entity_type'] . ':' . $ief_field_instance['bundle'] . ':' . $ief_field_instance['field_name'];
  $right_chain = $context['instance']['entity_type'] . ':' . $context['instance']['bundle'] . ':' . $context['instance']['field_name'];

  // Check if "hide" is enabled on the IEF that is holding this field.
  $hide_cer_fields = !empty($ief_field_instance['widget']['settings']['type_settings']['hide_cer_fields']);
  if ($hide_cer_fields) {
    $finder = new CerPresetFinder();
    $element['#access'] = !($finder
      ->find($left_chain, $right_chain)
      ->count()
      ->execute() || $finder
      ->find($right_chain, $left_chain, TRUE)
      ->count()
      ->execute());
  }
}