Coding standards
Security in ResourceSpace
Developer reference
Database
Action functions
Admin functions
Ajax functions
Annotation functions
API functions
Collections functions
Comment functions
Config functions
CSV export functions
Dash functions
Debug functions
Encryption functions
Facial recognition functions
File functions
General functions
Language functions
Log functions
Login functions
Message functions
Migration functions
Node functions
PDF functions
Plugin functions
Render functions
Reporting functions
Request functions
Research functions
Slideshow functions
Theme permission functions
User functions
Video functions
Database functions
Metadata functions
Resource functions
Search functions
Map functions
Job functions
Tab functions
Test functions

display_field()

Description

Displays a field in the resource edit interface.

This function handles the rendering of various types of fields based on their configurations and context.
It supports multiple modes, such as batch editing, and adjusts the display based on user permissions,
errors from previous saves, and whether the field is being copied from another resource.

Parameters

ColumnTypeDefaultDescription
$n int The index of the field in the form.
$field array The field configuration array containing settings and metadata for the field.
$newtab bool false Indicates if the field should be displayed in a new tab.
$modal bool false Indicates if the field is part of a modal dialog.

Location

include/render_functions.php lines 1799 to 2303

Definition

 
function display_field($n$field$newtab=false,$modal=false)
  {
  
debug_function_call(__FUNCTION__, [$n$field['ref'], $newtab$modal]);

  global 
$use$ref$original_fields$multiple$lastglobal,$is_template$language$lang,
  
$blank_edit_template$edit_autosave$errors$tabs_on_edit$collapsible_sections$ctrls_to_save,
  
$embedded_data_user_select$embedded_data_user_select_fields$show_error$save_errors$baseurl$is_search,
  
$all_selected_nodes,$original_nodes$FIXED_LIST_FIELD_TYPES$TEXT_FIELD_TYPES$DATE_FIELD_TYPES$upload_review_mode$check_edit_checksums$locked_fields$lastedited$copyfrom$fields;

  
// Set $is_search to false in case page request is not an ajax load and $is_search hs been set from the searchbar
  
$is_search=false;
  
  if(!isset(
$locked_fields))
    {
    
$locked_fields explode(",",getval("lockedfields",""));
    }

  if(!isset(
$copyfrom))
    {
    
$copyfrom getval('copyfrom''');
    }

  
$name="field_" $field["ref"];
  
$value=$field["value"];
  
$value=trim((string) $value);
  
$use_copyfrom=true;
  
$omit_when_copying_enacted=false;
  if (
$use != $ref && ($field["omit_when_copying"]))
        {
        
debug("display_field: reverting copied value for field " $field["ref"] . " as omit_when_copying is enabled");
        
# Return this field value back to the original value, instead of using the value from the copied resource/metadata template
        # This is triggered if field has the 'omit_when_copying' flag set
        
reset($original_fields);
        
$use_copyfrom=false;
        foreach (
$original_fields as $original_field)
            {
            if (
$original_field["ref"]==$field["ref"])
                {
                
$value=$original_field["value"];
                }
            }
        
$selected_nodes $original_nodes;
        
$omit_when_copying_enacted=true;
        }
    elseif((
$ref<|| $upload_review_mode) && isset($locked_fields) && in_array($field["ref"], $locked_fields) && $lastedited 0)
        {
        
// Get value from last edited resource 
        
debug("display_field: locked field " $field['ref'] . ". Using nodes from last resource edited - " $lastedited);
        
$selected_nodes get_resource_nodes($lastedited,$field["ref"]);
        }
    else
        {
        
$selected_nodes $all_selected_nodes;
        if (
in_array($field["type"], $DATE_FIELD_TYPES) && !$GLOBALS['use_native_input_for_date_field'])
            {
            
$submitted_val sanitize_date_field_input($field["ref"], false);
            }
        else
            {
            
$submitted_val getval("field_" $field['ref'], "");
            }
        if(!empty(
$save_errors) && $submitted_val != "")
            {
            
// Set to the value that was submitted 
            
$value $submitted_val;
            }
        }

    
$displaycondition=true;
    if (
$field["display_condition"] != "") {
        
// Check if field has a display condition set and render the client side check display condition functions
        
$displaycondition check_display_condition($n$field$fieldstrue$use);
        
debug(sprintf('$displaycondition = %s'json_encode($displaycondition)));
    }

    if (
$multiple && 
    ( (
getval("copyfrom","") == "" && getval('metadatatemplate''') == ""
      || 
str_replace(array(" ",","),"",(string)$value)=="") ) 
        {
        
debug('Blank the value for multi-edits unless copying data from resource');
        
$value="";
        }

    if (
$field["global"] == && $lastglobal && $collapsible_sections) { ?>
        </div>
        <h2 class="CollapsibleSectionHead" id="resource_type_properties"> echo escape($lang["typespecific"]); ?></h2>
        <div class="CollapsibleSection" id="ResourceProperties if ($ref<0) {echo "Upload";} ?>TypeSpecificSection">
        
    
}

    
# Blank form if 'reset form' has been clicked
    
if('' != getval('resetform'''))
        {
        
$value '';

        if(
in_array($field['type'], $FIXED_LIST_FIELD_TYPES))
            {
            
$selected_nodes = array();
            }
        
$user_set_values = array();
        }
    
// Copy from resource should only show values from the resource we are copying from
    
elseif($ref != $use && $copyfrom != '')
        {
        
$user_set_values = array();

        if(
in_array($field['type'], $FIXED_LIST_FIELD_TYPES))
            {
            
$field['nodes'] = array_filter($field['nodes'], 'node_is_active');
            }
        }
    else
        {
        
debug("display_field: getting all user selected values from form data for field " $field['ref']);
        
$user_set_values getval('nodes', [], false'is_array');
        
debug(sprintf('$user_set_values = %s'json_encode($user_set_values)));
        }

    
/****************************** Errors on saving ***************************************/
    
$field_save_error false;
    if (
        isset(
$show_error
        && isset(
$save_errors)
        && 
array_key_exists($field['ref'], $save_errors)
        ) {
        
$field_save_error true;
        }
     
    if (
$multiple && !hook("replace_edit_all_checkbox","",array($field["ref"])))
      {
      
# Multiple items, a toggle checkbox appears which activates the question
      
?>
      <div class="Question edit_multi_checkbox">
        <input name="editthis_ echo escape($name?>"
               id="editthis_ echo $n?>"
               type="checkbox"
               value="yes"
                if($field_save_error){?> checked ?>
               onClick="batch_edit_toggle_edit_multi_checkbox_question( echo (int) $n?>);"  if(getval("copyfrom","")!="" && $use_copyfrom && $value!=""){echo " checked" ;} ?>>&nbsp;
            <label for="editthis echo $n?>"> echo escape($field["title"]) ?></label>
            <div class="clearerleft"></div>
        </div>
        <!-- End of edit_multi_checkbox -->

      
}

  if (
$multiple && !hook("replace_edit_all_mode_select","",array($field["ref"])))
      {
      
# When editing multiple, give option to select Replace All Text or Find and Replace
      
$onchangejs '';
      if (
in_array($field['type'], $FIXED_LIST_FIELD_TYPES))
          {
          
$onchangejs 'setActionPromptText(' . (int) $n '); ';
          }
      
$onchangejs .= "var fr=document.getElementById('findreplace_" . (int) $n "');\n";
      
$onchangejs .= "var q=document.getElementById('question_" . (int) $n "');\n";
      if (
$field["type"] == FIELD_TYPE_CATEGORY_TREE)
        {
        
$onchangejs .= "if (this.value=='RM'){branch_limit_field['field_" $field["ref"] . "']=1;}else{branch_limit_field['field_" $field["ref"] . "']=0;}";
        }
      elseif (
in_array($field["type"], $TEXT_FIELD_TYPES ))
        {
        
$onchangejs .= "
        var cf=document.getElementById('copy_from_field_" 
$field["ref"] . "');
            if (this.value=='CF')
                {
                cf.style.display='block';q.style.display='none';fr.style.display='none';
                }
            else if (this.value=='FR')
                {
                fr.style.display='block';q.style.display='none';cf.style.display='none';
                }
            else
                {
                fr.style.display='none';cf.style.display='none';q.style.display='block';
                }"
;
        }
      
?>
      <div class="Question" id="modeselect_ echo $n?>" style=" if($value=="" && !$field_save_error ){echo "display:none;";} ?>padding-bottom:0;margin-bottom:0;">
      <label for="modeselectinput"> echo escape($lang["editmode"])?></label>
      <select id="modeselectinput_ echo $n?>" name="modeselect_ echo $field["ref"]; ?>" class="stdwidth" onChange=" echo $onchangejs;hook ("edit_all_mode_js"); ?>">
      <option value="RT"> echo escape($lang["replacealltext"])?></option>
      
      
if (in_array($field["type"], $TEXT_FIELD_TYPES ))
        {
        
# 'Find and replace', prepend and 'copy from field' options apply to text boxes only.
        
?>
        <option value="FR" if(getval("modeselect_" $field["ref"],"")=="FR"){?> selected ?>> echo escape($lang["findandreplace"])?></option>
        <option value="CF" if(getval("modeselect_" $field["ref"],"")=="CF"){?> selected ?>> echo escape($lang["edit_copy_from_field"])?></option>
        <option value="PP" if(getval("modeselect_" $field["ref"],"")=="PP"){?> selected ?>> echo escape($lang["prependtext"])?></option>
          
        
}
      if((
in_array($field['type'], $TEXT_FIELD_TYPES)) || in_array($field['type'], [FIELD_TYPE_CHECK_BOX_LISTFIELD_TYPE_CATEGORY_TREEFIELD_TYPE_DYNAMIC_KEYWORDS_LIST]))
        {
        
# Append applies to text boxes, checkboxes ,category tree and dynamic keyword fields onl.
        
?>
        <option value="AP" if(getval("modeselect_" $field["ref"],"")=="AP"){?> selected ?>> echo escape($lang["appendtext"])?></option>

        
}
      if (
        
# Remove applies to text boxes, checkboxes, dropdowns, category trees and dynamic keywords only. 
        
in_array($field['type'], array_merge($TEXT_FIELD_TYPES, array(FIELD_TYPE_CHECK_BOX_LISTFIELD_TYPE_DROP_DOWN_LISTFIELD_TYPE_CATEGORY_TREEFIELD_TYPE_DYNAMIC_KEYWORDS_LIST)))
        
# And it only applies if the field is optional
        
&& $field['required'] == 
        
) {
            
?> 
            <option value="RM" if(getval("modeselect_" $field["ref"],"")=="RM"){?> selected ?>> echo escape($lang["removetext"])?></option>
            
        
}
        
hook ("edit_all_extra_modes","",[$field]);
        
?>
        </select>
      </div><!-- End of modeselect_ echo $n?> -->

      
      
if (in_array($field["type"], $TEXT_FIELD_TYPES))
        {
        
render_field_selector_question("","copy_from_field_" $field["ref"], array(), "stdwidth"true);
        }
        
?>

      <div class="Question" id="findreplace_ echo $n?>" style="display:none;border-top:none;">
        <label>&nbsp;</label>
         echo escape($lang["find"])?> <input type="text" name="find_ echo $field["ref"]; ?>" class="shrtwidth">
         echo escape($lang["andreplacewith"])?> <input type="text" name="replace_ echo $field["ref"]; ?>" class="shrtwidth">
      </div><!-- End of findreplace_ echo $n?> -->

       hook ("edit_all_after_findreplace","",array($field,$n)); 
      }

      if (
$multiple && in_array($field['type'], $FIXED_LIST_FIELD_TYPES))
          {
          
?>
          <div class="Question" class="stdwidth" style="display:none;" id="displayexisting_ echo (int) $n?>" data-rtfid=" echo (int) $field['ref']; ?>">
            <div class="questionSpacer"></div>
            <p> echo escape($lang["edit_multiple_existing_values"]) . ':'?></p>
            <div class="questionSpacer"></div>
            <div>
              <div class="displayexisting_options" id="displayexisting_options_ echo (int) $n?>" data-questionid=" echo (int) $n?>"></div>
              <div class="questionSpacer"></div>
              <input type="button" id="displayexisting_options_more_ echo (int) $n?>" style="display:none;" value="+  echo escape($lang["edit_multiple_show_more"]); ?>" onclick="showMoreSelectedOptions( echo (int) $n?>);">
              </div>
          </div>
          
          
}
          
?>
      <div class="Question  if($upload_review_mode && in_array($field["ref"],$locked_fields)){echo " lockedQuestion ";} if($field_save_error) { echo 'FieldSaveError'; } ?>" id="question_ echo $n . ($multiple '' '_' $use); ?>"
      
if (($multiple && !$field_save_error) || !$displaycondition || $newtab)
        {
?>style="border-top:none; 
        
if (($multiple && $value=="") || !$displaycondition)
        {
        
debug('Hide this');
        
?>
        display:none;
        
        
}
        
?>"
        
}
     
?>>
     
     
if ($multiple && in_array($field['type'], $FIXED_LIST_FIELD_TYPES))
        {
        
?>
        <div id="ActionPrompt_ echo (int) $n?>">
          <div class="questionSpacer"></div>
          <p id="ActionPromptText_ echo (int) $n?>"></p>
       </div>
     
        
}
     
$labelname $name;

     
// For batch editing, CKEditor renders as a text box, as it does not work at all well when appending / prepending (it expects to work with HTML only)
     
if ($field['type'] == && $multiple)
        {
        
$field['type']=1;
        }
      
     
// Add _selector to label so it will keep working:
     
if($field['type'] == 9)
      {
      
$labelname .= '_selector';
      }

      
// Add -d to label so it will keep working
     
if($field['type'] == 4)
        {
        
$labelname .= '-d';
        }
        
?>
     <label for=" echo escape($labelname)?> if($field['type']==FIELD_TYPE_DATE_RANGE) {echo " class='daterangelabel'";} ?> >
      
     
if (!$multiple
        {
        echo 
escape($field["title"]);
        if (!
$is_template && $field["required"]==1)
            {
            echo 
"<sup>*</sup>";
            }
        } 
     if (
$upload_review_mode)
        {
        
renderLockButton($field["ref"], $locked_fields);
        }
        
?>
     </label>

     
    
# Autosave display
     
if ($edit_autosave || $ctrls_to_save)
      {
      
?>
      <div class="AutoSaveStatus">
      <span id="AutoSaveStatus echo $field["ref"]; ?>" style="display:none;"></span>
      </div>
      
      

    
# Define some Javascript for help actions (applies to all fields)
    # Help actions for CKEditor fields are set in pages/edit_fields/8.php
     
if (trim($field["help_text"]=="")) 
       {
        
# No helptext; so no javascript for toggling
        
$help_js="";
       }
     else
       {
       if ( 
in_array($field["type"],array(2,3,4,6,7,10,12,14)) )
         {
         
# For the selected field types the helptext is always shown; so no javascript toggling 
         
$help_js="";
         }
       else
         {
         
# For all other field types setup javascript to toggle helptext depending on loss or gain of focus
         
$help_js="onBlur=\"HideHelp(" $field["ref"] . ");return false;\" onFocus=\"ShowHelp(" $field["ref"] . ");return false;\"";
         }
       }

    
#hook to modify field type in special case. Returning zero (to get a standard text box) doesn't work, so return 1 for type 0, 2 for type 1, etc.
     
$modified_field_type="";
     
$modified_field_type=(hook("modifyfieldtype"));
     if (
$modified_field_type){$field["type"]=$modified_field_type-1;}

     
hook("addfieldextras");
    
# ----------------------------  Show field -----------------------------------
    
$type $field['type'];

    
// Default to text type.
    
if('' == $type)
        {
        
$type 0;
        }

    
// The visibility status (block/none) will be sent to the server for validation purposes
    
echo "<input id='field_" . (int) $field['ref']  . "_displayed' name='" "field_" . (int) $field['ref']  . "_displayed' type='hidden' value='block'>";

    if(!
hook('replacefield''', array($field['type'], $field['ref'], $n)))
        {
        global 
$auto_order_checkbox$auto_order_checkbox_case_insensitive$FIXED_LIST_FIELD_TYPES$is_search;
        
        
// Establish the full set of selected nodes to be rendered for this field
        // Do this only if the field's selected nodes haven't previously been adjusted to take account of omit_when_copying
        
if(!$omit_when_copying_enacted)
            {
            
$selected_nodes array_unique(array_merge($selected_nodes,get_resource_nodes($use$field['ref'])));
            }

        
debug(sprintf('$selected_nodes = %s'json_encode($selected_nodes)));

        if(
in_array($field['type'], $FIXED_LIST_FIELD_TYPES))
            {
            
$name "nodes[{$field['ref']}]";

            
// Sometimes we need to pass multiple options
            
if(in_array($field['type'], array(FIELD_TYPE_CHECK_BOX_LISTFIELD_TYPE_CATEGORY_TREE)))
                {
                
$name "nodes[{$field['ref']}][]";
                }
            elseif(
FIELD_TYPE_DYNAMIC_KEYWORDS_LIST == $field['type'])
                {
                
$name "field_{$field['ref']}";
                }

            
$field_nodes = array();
            foreach(
$selected_nodes as $selected_node)
                {
                
$node_data = array();
                if(
get_node($selected_node$node_data) && $node_data["resource_type_field"] != $field["ref"])
                    {
                    continue;
                    }

                
$field_nodes[] = $selected_node;
                unset(
$node_data);
                }
            
sort($field_nodes);
            
debug(sprintf('$field_nodes = %s'json_encode($field_nodes)));
            if(!
$multiple && getval("copyfrom","") == "" && getval('metadatatemplate''') == "" && $check_edit_checksums)
                {
                echo 
"<input id='field_" . (int) $field['ref']  . "_checksum' name='" "field_" . (int) $field['ref']  . "_checksum' type='hidden' value='" md5(implode(",",$field_nodes)) . "'>";
                echo 
"<input id='field_" . (int) $field['ref']  . "_currentval' name='" "field_" . (int) $field['ref']  . "_currentval' type='hidden' value='" implode(",",$field_nodes) . "'>";
                }
            }
        elseif(
$field['type']==FIELD_TYPE_DATE_RANGE && !$blank_edit_template && getval("copyfrom","") == "" && getval('metadatatemplate''') == "" && $check_edit_checksums)
            {
            
$field['nodes'] = get_nodes($field['ref'], nullfalse);
            
$field_nodes = array();
            foreach(
$selected_nodes as $selected_node)
                {
                if(
in_array($selected_node,array_column($field['nodes'],"ref")))
                    {
                    
$field_nodes[] = $selected_node;
                    }
                }
            
sort($field_nodes);         
            
debug(sprintf('$field_nodes = %s'json_encode($field_nodes)));
            echo 
"<input id='field_" . (int) $field['ref']  . "_checksum' name='" "field_" . (int) $field['ref']  . "_checksum' type='hidden' value='" md5(implode(",",$field_nodes)) . "'>";
            }
        elseif(!
$multiple && !$blank_edit_template && getval("copyfrom","")=="" && getval('metadatatemplate''') == "" && $check_edit_checksums)
            {
            echo 
"<input id='field_" . (int) $field['ref']  . "_checksum' name='" "field_" . (int) $field['ref']  . "_checksum' type='hidden' value='" md5(trim(preg_replace('/\s\s+/'' ', (string) $field['value']))) . "'>";
            }
        elseif (
$field['type'] === FIELD_TYPE_DATE && $GLOBALS['use_native_input_for_date_field'])
            {
            if (
$GLOBALS['blank_date_upload_template'] && $value !== '' && $ref <= 0)
                {
                
$value '';
                }
            }

        
$is_search false;

        include 
"edit_fields/{$type}.php";
        
$lastglobal $field['global']==1;
        }
        
    
# ----------------------------------------------------------------------------

    # Display any error messages from previous save
    
if (array_key_exists($field["ref"],$errors))
      {
       
?>
       <div class="FormError">!!  echo $errors[$field["ref"]]; ?> !!</div>
       
      
}

    if (
trim($field["help_text"]!=""))
     {
        
# Show inline help for this field.
        # For certain field types that have no obvious focus, the help always appears
       
?>
       <div class="FormHelp" style="padding:0; if ( in_array($field["type"],array(2,3,4,6,7,10,12,14)) ) { ?> clear:left; } else { ?> display:none; ?>" id="help_ echo $field["ref"]; ?>"><div class="FormHelpInner"> echo nl2br(trim(i18n_get_translated($field["help_text"])))?></div></div>

     
}
    if((
$embedded_data_user_select || (isset($embedded_data_user_select_fields) && in_array($field["ref"],$embedded_data_user_select_fields))) && ($ref<&& !$multiple))
    {
      
?>
      <table id="exif_ echo $field["ref"]; ?>" class="ExifOptions" cellpadding="3" cellspacing="3"  if ($embedded_data_user_select){?> style="display: none;"  ?>>                    
         <tbody>
           <tr>        
             <td>
                 echo "&nbsp;&nbsp;" escape($lang["embeddedvalue"]) . ": " ?>
             </td>
             <td width="10" valign="middle">
               <input type="radio" id="exif_extract_ echo $field["ref"]; ?>" name="exif_option_ echo $field["ref"]; ?>" value="yes" checked>
            </td>
            <td align="left" valign="middle">
               <label class="customFieldLabel" for="exif_extract_ echo $field["ref"]; ?>"> echo escape($lang["embedded_metadata_extract_option"]) ?></label>
            </td>


            <td width="10" valign="middle">
               <input type="radio" id="no_exif_ echo $field["ref"]; ?>" name="exif_option_ echo $field["ref"]; ?>" value="no">
            </td>
            <td align="left" valign="middle">
               <label class="customFieldLabel" for="no_exif_ echo $field["ref"]; ?>"> echo escape($lang["embedded_metadata_donot_extract_option"]) ?></label>
            </td>


            <td width="10" valign="middle">
               <input type="radio" id="exif_append_ echo $field["ref"]; ?>" name="exif_option_ echo $field["ref"]; ?>" value="append">
            </td>
            <td align="left" valign="middle">
               <label class="customFieldLabel" for="exif_append_ echo $field["ref"]; ?>"> echo escape($lang["embedded_metadata_append_option"]) ?></label>
            </td>


            <td width="10" valign="middle">
               <input type="radio" id="exif_prepend_ echo $field["ref"]; ?>" name="exif_option_ echo $field["ref"]; ?>" value="prepend">
            </td>
            <td align="left" valign="middle">
               <label class="customFieldLabel" for="exif_prepend_ echo $field["ref"]; ?>"> echo escape($lang["embedded_metadata_prepend_option"]) ?></label>
            </td>

         </tr>
      </tbody>
   </table>        
   
  
}
  
?>
  <div class="clearerleft"> </div>
  </div><!-- end of question_ echo $n?> div -->
       
  
  hook
('afterfielddisplay''', array($n$field));
  }

This article was last updated 17th November 2024 15:35 Europe/London time based on the source file dated 13th November 2024 14:05 Europe/London time.