Created
September 19, 2013 07:57
-
-
Save fieke/6620336 to your computer and use it in GitHub Desktop.
Hook form alter to filter reference CT in onother CT
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* Hook form_alter | |
*/ | |
function custom_form_alter(&$form, &$form_state, $form_id) { | |
switch ($form_id) { | |
// alter the dropdown for the products, used by a success story | |
case 'success_stories_node_form': | |
$language_map = array( | |
'nl' => array( | |
'node/59', | |
'node/60', | |
'node/97', | |
), | |
'fr' => array( | |
'node/184', | |
'node/185', | |
'node/186', | |
), | |
'en' => array( | |
'node/115', | |
'node/154', | |
'node/155', | |
), | |
); | |
$rtn = array(); | |
foreach ($language_map as $language_code => $paths) { | |
$rtn[$language_code] = array(); | |
$tmp = array(); | |
foreach ($paths as $path) { | |
if ($children = _custom_get_menu_children($path)) { | |
_custom_refactor_children($tmp, $children); | |
} | |
} | |
$rtn[$language_code] = $tmp; | |
} | |
$form['field_related_product'][LANGUAGE_NONE]['#options'] = $rtn; | |
break; | |
} | |
} | |
/** | |
* Refactor the menu children for display in the select list | |
* | |
* @param Array $tmp | |
* the target array | |
* @param Array $children | |
* the menu children | |
* @param int $lvl | |
* what level you are refactoring | |
*/ | |
function _custom_refactor_children(&$tmp, $children, $lvl = 0) { | |
foreach ($children as $nid => $child) { | |
$prefix = str_repeat("--", $lvl); | |
$tmp[$nid] = $prefix . $child['title']; | |
_custom_refactor_children($tmp, $child['children'], $lvl + 1); | |
} | |
} | |
/** | |
* Check if the parent can be found and return the children | |
* | |
* @param String $path | |
* @return | |
* the chidlren | |
* FALSE on failure | |
*/ | |
function _custom_get_menu_children($path) { | |
$plid = db_query("SELECT mlid FROM menu_links WHERE link_path=:path", array( | |
':path' => $path, | |
))->fetchField(); | |
if ($plid) | |
return _custom_get_menu_children_from_plid($plid); | |
else | |
return FALSE; | |
} | |
/** | |
* Get a full list of children | |
* | |
* @param int $plid | |
* @return Array | |
* the children keyed by nid | |
* values: title, children | |
*/ | |
function _custom_get_menu_children_from_plid($plid) { | |
$mlids = db_query("SELECT mlid FROM menu_links WHERE plid=:plid", array( | |
':plid' => $plid, | |
))->fetchCol(); | |
foreach (array_values($mlids) as $mlid) { | |
$nid = db_query("SELECT link_path FROM menu_links WHERE mlid=:mlid", array( | |
':mlid' => $mlid, | |
))->fetchField(); | |
if (!$nid) | |
break; | |
$nid = str_replace("node/", "", $nid); | |
if (is_numeric($nid)) { | |
$rtn[$nid] = array( | |
'title' => db_query("SELECT title FROM node WHERE nid=:nid", array( | |
':nid' => $nid, | |
))->fetchField(), | |
'children' => _custom_get_menu_children_from_plid($mlid) | |
); | |
} | |
} | |
return $rtn; | |
} | |
function custom_submenu_tree_all_data($path, $menu = 'main-menu', $curr_level = 0, $rebuilt_path = '', $childtree = array()) { | |
$tree = menu_tree_all_data($menu); | |
$args = explode('/', $path); | |
$rebuilt_path = empty($rebuilt_path) ? $args[$curr_level] : $rebuilt_path . '/' . $args[$curr_level]; | |
foreach ($tree as $branch) { | |
if ($branch['link']['link_path'] == $rebuilt_path) { | |
$childtree = $branch['below']; | |
if ($rebuilt_path != $path) { | |
$curr_level++; | |
MY_MODULE_submenu_tree_all_data($path, $menu, $curr_level, $rebuilt_path, $childtree); | |
} | |
} | |
} | |
$items = array(); | |
foreach ($childtree as $child) { | |
$items[] = l($child['link']['title'], $child['link']['link_path']); | |
} | |
return theme('item_list', array('items' => $items, 'attributes' => array(), 'type' => 'ul')); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment