0

I created a content type for a new block. One of the fields in this needs to pull in category information from a magento site. I have code to get all of the categories and display them in a drop-down working in a panel template. The only step I am missing is know how to access this as a field in my new content type. I have read some articles about using something called hooks for this process but I'm not fully sure about how they work. Is a hook the best method and if so what exactly does it involve programmatically?

3
  • Do you mean ctools content types? In your edit form you need to save the value which later will be available in $conf, for a full explanation see this page for example pixelite.co.nz/article/… Commented Jun 5, 2014 at 11:13
  • It's just a regular content type. I don't think ctools content types is suitable for what I'm trying to do. Commented Jun 5, 2014 at 11:37
  • I would create a textfield in your content type. Then use hook_form_alter to change the textfield into a select field. I would use a textfield instead of list(text) because your allowed values will change depending on the response from the other site. Check out api.drupal.org/api/drupal/modules!system!system.api.php/… you need to change the form elements #type and add #options Commented Jun 5, 2014 at 11:58

1 Answer 1

0

As mentioned in one of the comments I used the hook_form_alter but instead of pulling in the panel I just created another function to get the information and used that as the options for my select.

  function gacms_magecp_content_type_edit_form($form, &$form_state) {

   $conf = $form_state['conf'];
   $categories = gacms_magecp_categories();

    $form['magecp'] = array(
     '#type' => 'select',
     '#options' => $categories,
     '#title' => t('Mage Category Product'),
     '#default_value' => isset($conf['magecp'])?$conf['magecp']:'',
     '#format' => isset($conf['format'])?$conf['format']:'full_html',
    );

    return $form;
  }

   function gacms_magecp_categories(){
    db_set_active('magento');

    $result = db_query("SELECT entity_id, name FROM catalog_category_flat_store_1");
        while ($row = $result->fetchAssoc()) {
           $content[$row['entity_id']]= $row['name'];
        }
        db_set_active();
        return $content;
    }

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.