You are here

function flag_field_attach_form in Flag 7.3

Same name and namespace in other branches
  1. 7.2 flag.module \flag_field_attach_form()

Implements hook_field_attach_form().

Handles the 'show_on_form' flag option.

Warning: will not work on entity types that are not fieldable, as this relies on a field module hook.

See also

flag_field_attach_submit()

File

./flag.module, line 742
The Flag module.

Code

function flag_field_attach_form($entity_type, $entity, &$form, &$form_state, $langcode) {
  list($id) = entity_extract_ids($entity_type, $entity);

  // Some modules are being stupid here. Commerce!
  if (empty($id)) {
    $id = NULL;
  }

  // Keep track of whether the entity is new or not, as we're about to fiddle
  // with the entity id for the flag's entity cache.
  $is_existing_entity = !empty($id);

  // Get all possible flags for this entity type.
  $flags = flag_get_flags($entity_type);

  // Filter out flags which need to be included on the node form.
  $flags_in_form = 0;
  $flags_visible = 0;
  foreach ($flags as $flag) {
    if (!$flag->show_on_form) {
      continue;
    }

    // Get the flag status.
    if ($is_existing_entity) {
      $flag_status = $flag
        ->is_flagged($id);
    }
    else {

      // We don't have per-bundle defaults on general entities yet: default
      // status is just unflagged.
      $flag_status = FALSE;

      // Apply the per-bundle defaults for nodes.
      if ($entity_type == 'node') {
        $node_type = $entity->type;
        $flag_status = variable_get('flag_' . $flag->name . '_default_' . $node_type, 0);
      }

      // For a new, unsaved entity, make a dummy entity ID so that the flag
      // handler can remember the entity. This allows access to the flag to be
      // correctly handled in node and comment preview.
      $id = 'new';
      $flag
        ->remember_entity($id, $entity);
    }

    // If the flag is not global and the user doesn't have access, skip it.
    // Global flags have their value set even if the user doesn't have access
    // to it, similar to the way "published" and "promote" keep the default
    // values even if the user doesn't have "administer nodes" permission.
    // Furthermore, a global flag is set to its default value on new nodes
    // even if the user creating the node doesn't have access to the flag.
    global $user;
    $access = $flag
      ->access($id, $flag_status ? 'unflag' : 'flag');
    if (!$access && !$flag->global) {
      continue;
    }
    $form['flag'][$flag->name] = array(
      '#type' => 'checkbox',
      '#title' => $flag
        ->get_label('flag_short', $id),
      '#description' => $flag
        ->get_label('flag_long', $id),
      '#default_value' => $flag_status,
      '#return_value' => 1,
      // Used by our drupalSetSummary() on vertical tabs.
      '#attributes' => array(
        'title' => $flag
          ->get_title(),
      ),
    );

    // If the user does not have access to the flag, set as a value.
    if (!$access) {
      $form['flag'][$flag->name]['#type'] = 'value';
      $form['flag'][$flag->name]['#value'] = $flag_status;
    }
    else {
      $flags_visible++;
    }
    $flags_in_form++;
  }
  if ($flags_in_form) {
    $form['flag'] += array(
      '#weight' => 1,
      '#tree' => TRUE,
    );
  }
  if ($flags_visible) {
    $form['flag'] += array(
      '#type' => 'fieldset',
      '#title' => t('Flags'),
      '#collapsible' => TRUE,
    );
    if ($entity_type == 'node') {

      // Turn the fieldset into a vertical tab.
      $form['flag'] += array(
        '#group' => 'additional_settings',
        '#attributes' => array(
          'class' => array(
            'flag-fieldset',
          ),
        ),
        '#attached' => array(
          'js' => array(
            'vertical-tabs' => drupal_get_path('module', 'flag') . '/theme/flag-admin.js',
          ),
        ),
      );
    }
  }
}