1

In an entity-type with term-reference i want to add a computed field, which grabs the term and sanitizes it for further use with the drupal_clean_css_identifier for example. My code doesn't work, because field_get_items gives back a full link to the term instead the term name as plaintext:

$entity_field[0]['value'] = drupal_clean_css_identifier(array_pop(field_get_items($entity_type, $entity, 'field_myterms'))));

Can someone help me out?

3

calling field_get_items() on a term reference field returns an array like this

Array(
  0 => Array(
    'tid' => 10,
  ),
)

to get the term name from the term id (tid) you should load the taxonomy term:

$field_myterms = field_get_items($entity_type, $entity, 'field_myterms');
if (!empty($field_myterms)) {
  $term = taxonomy_term_load($field_myterms[0]['tid']);
}

$term is an stdClass object:

stdClass Object
(
    [tid] => 1
    [vid] => 1
    [name] => test
    [description] => 
    [format] => filtered_html
    [weight] => 0
    [vocabulary_machine_name] => tags
    [rdf_mapping] => Array
        (
            [rdftype] => Array
                (
                    [0] => skos:Concept
                )

            [name] => Array
                (
                    [predicates] => Array
                        (
                            [0] => rdfs:label
                            [1] => skos:prefLabel
                        )

                )

            [description] => Array
                (
                    [predicates] => Array
                        (
                            [0] => skos:definition
                        )

                )

            [vid] => Array
                (
                    [predicates] => Array
                        (
                            [0] => skos:inScheme
                        )

                    [type] => rel
                )

            [parent] => Array
                (
                    [predicates] => Array
                        (
                            [0] => skos:broader
                        )

                    [type] => rel
                )

        )

)

to set the computed field value:

$entity_field[0]['value'] = drupal_clean_css_identifier($term->name);
| improve this answer | |
  • Thanks a lot! Your answer is perfect, but for now i can't give you any credit-points 'cause i've got not enough reputation … – Madam Adam Nov 13 '12 at 13:35
  • Finally, here are your credits! – Madam Adam Nov 20 '12 at 18:43

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.