I would like to know how to get parent terms only, for example "Ages 0-2" and also print in dropdown.

This is the structure of the form array.

  [#info] => Array(
    [filter-tid] => Array(
      [operator] => tid_op
      [value] => tid
      [label] => Term
      [description] => 
  [tid] => Array(
    [#type] => select
    [#options] => Array(
      [All] => - Any -
      [0] => stdClass Object(
        [option] => Array(
          [367] => Ages 0-2
      [1] => stdClass Object(
        [option] => Array(
          [2378] => -Infant

This is the code I am using.

  if ($form['#id']== "views-exposed-form-quick-find") { 
    $test = $form['tid']['#options'];
    foreach ($test as $tid) {
      // It prints "Array" and the "Trying to get property of non-object" message.
      print $tid->option;

I do not understand why the "Array" text repeats; it could be I am not pointing to right array.

How do I remove all children which do not have "age 12 - 14" (see "-" in the middle not from start)?

  • Hello, and welcome on Drupal Answers. May you show more of the code you are using? In particular, which function/hook contains that code? – kiamlaluno Nov 25 '12 at 21:17

What you obtain from that code is exactly what you should obtain.

The values you are getting from that control statement are:

  • $form['tid']['#options']['All'] is a string, and not an object. Trying to access $form['tid']['#options']['All']->option gives you a warning about accessing a property for something that is not an object.
  • $form['tid']['#options'][0] is an object, and it doesn't raise an error, but $form['tid']['#options'][0]->option is an array, and print $form['tid']['#options'][0]->option converts it in a string. That is why you see Array.
  • The same is true for $form['tid']['#options'][1].

To remove the array element that contains 'Age 12 - 14' from $form['tid']['#options'], I would use the following code.

foreach ($form['tid']['#options'] as $index => $tid) {
  if (is_object($tid) && !empty($tid->option) && is_array($tid->option) && in_array('Ages 12-14', $tid->option)) {

Notice that in_array() is case-sensitive. If you are searching for 'Age 12 - 14', but the string is 'age 12 - 14', the code doesn't remove the option.

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.