0

I've got a module that is creating fields programatically, all working so far. The code is below. This may be useful as-is if you are trying to create checkboxe fields from an array of values:

    /**
     * Helper function to create fields for workflow approvers
     *
     */

    function _create_approval_fields($approverRolesClean){
      $fieldPrefix = 'wf_';

      // TODO: If an existing field is removed, delete it.

      foreach ($approverRolesClean as $approverRole) {
        $cleanRole = preg_replace('/[^a-z]/', "", strtolower($approverRole));
        $fieldName = $fieldPrefix.$cleanRole;
        if (!field_info_field($fieldName)) {
          $field = array(
            'field_name' => $fieldName, 
            'type' => 'list_boolean',
            'widget' => array(
              'type' => 'options_onoff',
              'settings' => array(
                'display_label' => '1',
              ),
            ),
            'settings' => array(
              'allowed_values' => array(
                '0' => 'No',
                '1' => 'Yes',
                ),
              ),
            );
          field_create_field($field);
        }
        //Hardcoded:
        if (!field_info_instance('node', $fieldName, 'contenttype')) {
          $instance = array(
            'field_name' => $fieldName, 
            'entity_type' => 'node', 
            'label' => 'Approved by ' . $approverRole, 
            //Hardcoded:
            'bundle' => 'contenttype',
            'widget' => array(
              'type' => 'options_onoff',
              'settings' => array(
                'display_label' => '1',
              ),
            ),
          );
          field_create_instance($instance);
        }
      }
    }

I set the value of the fields outside the Edit screen - so they never need to show there. I'm sure there is a settings parameter that I can add to hide it, but not sure where to find the doc. I don't think no_ui is what I need.

Otherwise I can use the form api to hide it at render time, but that doesn't seem as elegant.

Thanks,

Jack

0

If it's a regular Field API field, you can easily hide it with

hook_field_access

e.g:

/**
 * Implements hook_field_access
 */
function mymodule_field_access($op, $field, $entity_type, $entity, $account) {
  if ($field['field_name'] == 'field_wf_admin' && $op == 'edit') {
    // Only allow admins to edit field value
    return user_access('administer nodes', $account);
  }
  return TRUE;
}

Alternatively, there's the field_extrawidgets module for hidden and readonly Field API fields

| improve this answer | |
1

I don't follow the example 100%, but to answer the question - in D7 you can set '#type' => 'hidden', '#value' => 'foo' or just do '#my_custom_key' => 'my custom value' and it will be available to you in the submit handler (or elsewhere, such as hook_field_attach_submit()).

| improve this answer | |
  • Thanks Charlie, the thing is I use the field as a checkbox in a different place. The content is boolean (checkbox makes sense). I just want to hide it on the edit screen. In the interim, I've just used a hook_form_alter() to hide them, that may be the best solution. – jackmcintyre Nov 14 '12 at 6:52
  • Simple and functional! Two useful pieces of info for 'type' => 'hidden' fields: its Drupal API specs and a way to render them with id attribute. – tanius Jul 13 '16 at 18:03

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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