You are here

function domain_entity_ui_submit in Domain Access Entity 7

Submit handler for the domain entity form.

Parameters

array $form: Form array.

array $form_state: Form state array.

File

./domain_entity.admin.inc, line 195
Domain Access Entity admininistration settings / form

Code

function domain_entity_ui_submit($form, &$form_state) {
  $values = $form_state['values'];
  $old_domain_entity_allowed_entity_types = variable_get('domain_entity_allowed_entity_types', array());

  // Save the disable access conditions value.
  variable_set('domain_entity_bypass_access_conditions', $values['domain_entity_bypass_access_conditions']);

  // Do not allow domain access on entity that are actually bundle.
  $entities_info = entity_get_info();
  foreach ($entities_info as $entity_type => $entity_info) {
    if (isset($entity_info['bundle of']) || !isset($entity_info['entity keys']['bundle']) || empty($entity_info['entity keys']['bundle'])) {
      unset($entities_info[$entity_type]);
    }
  }

  // Domain access take care of it:
  unset($entities_info['node']);

  // Just a bundle entity:
  unset($entities_info['taxonomy_vocabulary']);

  // Set the form.
  $domain_entity_allowed_entity_types = array();
  foreach ($entities_info as $entity_name => $entity_info) {
    if ($values[$entity_name . '_value']) {
      $domain_entity_allowed_entity_types[$entity_name] = array();
      foreach ($entity_info['bundles'] as $bundle_name => $bundle_info) {
        $default_values = array();
        foreach ($values[$entity_name . '_' . $bundle_name . '_default_values'] as $key => $value) {
          if ($key == $value && $key !== DOMAIN_ACTIVE && $key !== DOMAIN_ALL) {
            $default_values[$key] = $value;
          }
          if ($key === DOMAIN_ACTIVE && $value === DOMAIN_ACTIVE) {
            $default_values = array(
              $key => $value,
            );
            break;
          }
          if ($key === DOMAIN_ALL && $value === DOMAIN_ALL) {
            $default_values = array(
              $key => $value,
            );
            break;
          }
        }

        // Change the widget of the fields if needed.
        $domain_entity_allowed_entity_types[$entity_name][$bundle_name] = array(
          $values[$entity_name . '_' . $bundle_name . '_behavior'] => $default_values,
        );
        if (in_array($entity_name, array_keys($old_domain_entity_allowed_entity_types))) {
          foreach ($domain_entity_allowed_entity_types[$entity_name] as $bundle_name => $bundle_info) {
            if (isset($old_domain_entity_allowed_entity_types[$entity_name][$bundle_name]) && key($bundle_info) != key($old_domain_entity_allowed_entity_types[$entity_name][$bundle_name])) {
              $field_name = domain_entity_get_entity_field_name($entity_name);
              $instance = field_read_instance($entity_name, $field_name, $bundle_name);
              $instance['widget']['type'] = key($bundle_info);
              field_update_instance($instance);
            }
          }
        }
      }
    }
  }

  // Uninstall disabled entity type (remove the field and instances).
  if (!empty($old_domain_entity_allowed_entity_types)) {
    $uninstall_entity_types = array_diff(array_keys($old_domain_entity_allowed_entity_types), array_keys($domain_entity_allowed_entity_types));
    if (!empty($uninstall_entity_types)) {
      foreach ($uninstall_entity_types as $uninstall_entity_type) {
        $field_name = domain_entity_get_entity_field_name($uninstall_entity_type);
        foreach ($old_domain_entity_allowed_entity_types[$uninstall_entity_type] as $bundle_name => $data) {
          $instance = field_info_instance($uninstall_entity_type, $field_name, $bundle_name);
          field_delete_instance($instance, TRUE);
          drupal_set_message(t('Domain access has been disabled on %entity_name', array(
            '%entity_name' => $uninstall_entity_type,
          )));
        }
      }
      field_delete_field($field_name);
    }
  }
  variable_set('domain_entity_allowed_entity_types', $domain_entity_allowed_entity_types);
  domain_entity_types_enable_domain_field($domain_entity_allowed_entity_types);
  drupal_set_message(t('Domain entity settings updated'));
}