0

I have been looking if there is a function that return all the possible values for a field. I have a select field that is a Node Ref. So, function list_allowed_values() is out of the question. Because, list_allowed_values() is not compatible with Node Ref. Does anyone know a function to get all the possible Node Ref for a field?

Or do have to code my own function? Thanks, Nate

1
  • please post your answer separately Commented Dec 8, 2013 at 9:19

1 Answer 1

-1

Ok, I did some digger on google. ychaugule made a nice function for this problem. Here is the post. Also, reference to the information here:

"But the above code will only work for select list, not for node reference or other types of references. _field_allowed_values() function is a custom function which returns allowed values of reference type field. The function returns allowed values for node reference and term reference field and can be extended to get allowed values of other reference fields as well. - See more at: http://yogeshchaugule.com/blog/2013/get-allowed-values-select-list-field#sthash.xnf89Cgn.dpuf"

<?php function _field_allowed_values($field_name) { // retrieve field configurations from DB. $allowed_values = array(); $query = db_select('field_config'); $query->condition('field_name',$field_name) ->fields('field_config',array('type', 'data')); $result = $query->execute()->fetchAssoc(); // Allowed references type $ref_type = array( 'node_reference', 'taxonomy_term_reference', ); $field_config = unserialize($result['data']); if (in_array($result['type'], $ref_type)) { if ($result['type'] == 'node_reference') { $node_types = array_filter($field_config['settings']['referenceable_types']); $query = db_select('node'); $query->condition('type', $node_types, 'in') ->condition('status', 1) ->fields('node', array('nid', 'title')); $allowed_values = $query->execute()->fetchAllKeyed(0, 1); } elseif ($result['type'] == 'taxonomy_term_reference') { $allowed_values = taxonomy_allowed_values($field_config); } } else { $allowed_values_raw = unserialize($result['data']); $allowed_values = (!empty($allowed_values_raw['settings']['allowed_values']) ? $allowed_values_raw['settings']['allowed_values'] : array()); } return $allowed_values; } $field = _field_allowed_values('FIELD_NAME'); 
2
  • 1
    While this link may answer the question, it is better to include the essential parts of the answer here and provide the link for reference. Link-only answers can become invalid if the linked page changes. Commented Dec 9, 2013 at 21:15
  • I updated the answer as requested. Commented Dec 9, 2013 at 22:35

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.