CiviCRM Community Forums (archive)

*

News:

Have a question about CiviCRM?
Get it answered quickly at the new
CiviCRM Stack Exchange Q+A site

This forum was archived on 25 November 2017. Learn more.
How to get involved.
What to do if you think you've found a bug.



  • CiviCRM Community Forums (archive) »
  • Old sections (read-only, deprecated) »
  • Developer Discussion (Moderator: Donald Lobo) »
  • Finding Custom Field ID
Pages: [1]

Author Topic: Finding Custom Field ID  (Read 760 times)

Chris Burgess

  • Ask me questions
  • ****
  • Posts: 675
  • Karma: 59
Finding Custom Field ID
March 30, 2009, 05:12:09 pm
In http://forum.civicrm.org/index.php/topic,3695.html I posted an additional function for CRM_Core_BAO_CustomField which would make it simple to look up a custom field ID by looking up the field using its name and / or label. That code was against 2.0, haven't tested it yet against 2.2.

I'm porting that code across to 2.2 today, but I wonder if there is a simpler method to achieve the same result in core CiviCRM?

Code: [Select]
    /**
     * Make it possible to find a generated custom field ID by looking
     * up the field's label / column name
     *
     * @param array details to identify the custom field - ideally,
     * custom group name as well as either custom field label or name,
     * but custom group name / title is not required
     *
     * @return custom field ID
     */
    public static function getCustomFieldId( $params ) {
      if ( !is_array( $params ) ||
   ( !isset( $params['field_name'] ) &&
     !isset( $params['field_label'] ) ) ) {
return civicrm_create_error( "Custom field name or label is required" ) ;
      }
      $args = $where = array() ;
      if ( isset( $params['group_title'] ) || isset( $params['group_name'] ) ) {
$join = "JOIN civicrm_custom_group ON civicrm_custom_group.id = civicrm_custom_field.custom_group_id" ;
if ( isset( $params['group_title'] ) ) {
  $where[] = "civicrm_custom_group.title = %1" ;
  $args[1] = array( $params['group_title'], 'String' ) ;
}
if ( isset( $params['group_name'] ) ) {
  $where[] = "civicrm_custom_group.name = %2" ;
  $args[2] = array( $params['group_name'], 'String' ) ;
}
      }
      if ( isset( $params['field_name'] ) ) {
$where[] = "civicrm_custom_field.column_name = %3" ;
$args[3] = array( $params['field_name'], 'String' )  ;
      }
      if ( isset( $params['field_label'] ) ) {
$where[] = "civicrm_custom_field.column_name = %4" ;
$args[4] = array( $params['field_label'], 'String' ) ;
      }
      $sql = "SELECT civicrm_custom_field.id FROM civicrm_custom_field " .
$join .
" WHERE " . implode( " AND ", $where ) ;
      return CRM_Core_DAO::singleValueQuery( $sql, $args );
    }
@xurizaemon ● www.fuzion.co.nz

Donald Lobo

  • Administrator
  • I’m (like) Lobo ;)
  • *****
  • Posts: 15963
  • Karma: 470
    • CiviCRM site
  • CiviCRM version: 4.2+
  • CMS version: Drupal 7, Joomla 2.5+
  • MySQL version: 5.5.x
  • PHP version: 5.4.x
Re: Finding Custom Field ID
March 30, 2009, 08:06:15 pm

i dont recall a function for the below existing within CiviCRM, since internally we always use custom field ids to refer to the fields.

lobo
A new CiviCRM Q&A resource needs YOUR help to get started. Visit our StackExchange proposed site, sign up and vote on 5 questions

Pages: [1]
  • CiviCRM Community Forums (archive) »
  • Old sections (read-only, deprecated) »
  • Developer Discussion (Moderator: Donald Lobo) »
  • Finding Custom Field ID

This forum was archived on 2017-11-26.