You are here

function nodeaccess_userreference_form_field_ui_field_edit_form_alter in Node access user reference 7.3

Same name and namespace in other branches
  1. 7.2 nodeaccess_userreference.module \nodeaccess_userreference_form_field_ui_field_edit_form_alter()

Implements hook_form-FORM-ID_alter().

File

./nodeaccess_userreference.module, line 22
The Node access user reference module.

Code

function nodeaccess_userreference_form_field_ui_field_edit_form_alter(&$form, $form_state) {
  if (($form['#field']['type'] == 'entityreference' && $form['#field']['settings']['target_type'] == 'user' || $form['#field']['type'] == 'user_reference') && $form['#instance']['entity_type'] == 'node') {
    $data = isset($form['#instance']['settings']['nodeaccess_userreference']) ? $form['#instance']['settings']['nodeaccess_userreference'] : array();
    $current = nodeaccess_userreference_field_settings($form['#instance']['bundle'], $form['#instance']['field_name']);
    $form['instance']['settings']['nodeaccess_userreference'] = array(
      '#type' => 'fieldset',
      '#title' => t('Node access user reference'),
      '#tree' => TRUE,
      '#collapsible' => TRUE,
      '#collapsed' => empty($current),
    );
    $referenced_states = array(
      array(
        '[name="instance[settings][nodeaccess_userreference][referenced][view]"]' => array(
          'checked' => TRUE,
        ),
      ),
      array(
        '[name="instance[settings][nodeaccess_userreference][referenced][update]"]' => array(
          'checked' => TRUE,
        ),
      ),
      array(
        '[name="instance[settings][nodeaccess_userreference][referenced][delete]"]' => array(
          'checked' => TRUE,
        ),
      ),
    );
    $form['instance']['settings']['nodeaccess_userreference']['referenced'] = array(
      '#type' => 'checkboxes',
      '#title' => t('Grants for referenced users on the node'),
      '#default_value' => isset($data['referenced']) ? $data['referenced'] : array(),
      '#options' => array(
        'view' => t('View'),
        'update' => t('Update'),
        'delete' => t('Delete'),
        'deny_view' => t('Deny view'),
        'deny_update' => t('Deny update'),
        'deny_delete' => t('Deny delete'),
      ),
      '#description' => t('These content access permissions will be granted to users referenced in the field.  These permissions apply to the node on which the field values are set.  Do not check the <em>deny</em> options unless you want to forcibly deny access to referenced users.'),
    );
    $form['instance']['settings']['nodeaccess_userreference']['referenced_published'] = array(
      '#type' => 'select',
      '#title' => t('Unpublished nodes'),
      '#title_display' => 'none',
      '#default_value' => isset($data['referenced_published']) ? $data['referenced_published'] : 0,
      '#options' => array(
        0 => t('Give these grants for published nodes only'),
        1 => t('Give these grants for published and unpublished nodes'),
      ),
    );
    $form['instance']['settings']['nodeaccess_userreference']['create'] = array(
      '#type' => 'checkboxes',
      '#title' => t('Grants for referenced users to create content'),
      '#default_value' => isset($data['create']) ? $data['create'] : array(),
      '#options' => node_type_get_names(),
      '#description' => t('These content access permissions will be granted to users referenced in the field.  These permissions apply to the content types selected.'),
    );
    $form['instance']['settings']['nodeaccess_userreference']['author'] = array(
      '#type' => 'checkboxes',
      '#title' => t('Grants for author'),
      '#default_value' => isset($data['author']) ? $data['author'] : array(),
      '#options' => array(
        'view' => t('View'),
        'update' => t('Update'),
        'delete' => t('Delete'),
      ),
      '#description' => t('These content access permissions will be granted to the authors of nodes.'),
      '#states' => array(
        'visible' => $referenced_states,
      ),
    );
    $form['instance']['settings']['nodeaccess_userreference']['author_published'] = array(
      '#type' => 'select',
      '#title' => t('Unpublished nodes'),
      '#title_display' => 'none',
      '#default_value' => isset($data['author_published']) ? $data['author_published'] : 0,
      '#options' => array(
        0 => t('Give these grants for published nodes only'),
        1 => t('Give these grants for published and unpublished nodes'),
      ),
      '#states' => array(
        'visible' => $referenced_states,
      ),
    );
    $form['instance']['settings']['nodeaccess_userreference']['all'] = array(
      '#type' => 'checkboxes',
      '#title' => t('Grants for all users'),
      '#default_value' => isset($data['all']) ? $data['all'] : array(),
      '#options' => array(
        'view' => t('View'),
      ),
      '#description' => t('These content access permissions will be granted to all users including %anon.', array(
        '%anon' => format_username(drupal_anonymous_user()),
      )),
      '#states' => array(
        'visible' => $referenced_states,
      ),
    );
    $form['instance']['settings']['nodeaccess_userreference']['all_published'] = array(
      '#type' => 'select',
      '#title' => t('Unpublished nodes'),
      '#title_display' => 'none',
      '#default_value' => isset($data['all_published']) ? $data['all_published'] : 0,
      '#options' => array(
        0 => t('Give these grants for published nodes only'),
        1 => t('Give these grants for published and unpublished nodes'),
      ),
      '#states' => array(
        'visible' => $referenced_states,
      ),
    );
    $form['instance']['settings']['nodeaccess_userreference']['unused'] = array(
      '#type' => 'radios',
      '#title' => t('When to set grants'),
      '#default_value' => isset($data['unused']) ? $data['unused'] : 0,
      '#options' => array(
        0 => t('When the user reference field is in use'),
        1 => t('Always'),
      ),
      '#description' => t('Determines whether to set grants when the field is not in use.'),
      '#states' => array(
        'visible' => $referenced_states,
      ),
    );
    if (module_exists('views')) {
      $form['instance']['settings']['nodeaccess_userreference']['views'] = array(
        '#type' => 'fieldset',
        '#title' => t('Views - Nodes to use'),
        '#collapsible' => TRUE,
        '#collapsed' => !isset($data['views']['view']) || $data['views']['view'] == '',
        '#description' => t('Node access user reference looks at user reference values from all nodes in ' . 'this content type using this user reference field.  You can, ' . 'however, choose to use values from only certain nodes by creating a views ' . 'display of nodes that match your criteria, and then selecting it ' . 'here.'),
      );
      $form['instance']['settings']['nodeaccess_userreference']['views']['view'] = array(
        '#type' => 'select',
        '#title' => t('View'),
        '#default_value' => isset($data['views']['view']) ? $data['views']['view'] : '',
        '#options' => nodeaccess_userreference_views_displays('node'),
      );
      $form['instance']['settings']['nodeaccess_userreference']['views']['view_args'] = array(
        '#type' => 'textfield',
        '#title' => t('View arguments'),
        '#default_value' => isset($data['views']['view_args']) ? $data['views']['view_args'] : '',
        '#description' => t('Provide a comma separated list of arguments to pass to the view.'),
      );
    }
    else {
      $form['instance']['settings']['nodeaccess_userreference']['views']['view'] = array(
        '#type' => 'value',
        '#value' => isset($data['views']['view']) ? $data['views']['view'] : '',
      );
      $form['instance']['settings']['nodeaccess_userreference']['views']['view_args'] = array(
        '#type' => 'value',
        '#value' => isset($data['views']['view_args']) ? $data['views']['view_args'] : '',
      );
    }
    $form['instance']['settings']['nodeaccess_userreference']['priority'] = array(
      '#type' => 'weight',
      '#title' => t('Priority'),
      '#default_value' => isset($data['priority']) ? $data['priority'] : 0,
      '#description' => t('It is recommended to always leave this set to 0.'),
      '#states' => array(
        'visible' => $referenced_states,
      ),
    );
  }
}