You are here

menu_editor_nodereference.module in Menu Editor 6.3

File

me_nodereference/menu_editor_nodereference.module
View source
<?php

/**
 * Implementation of class_menu_editor_Listener
 * (which is a new type of hook, hehe)
 */
class menu_editor_nodereference_class_menu_editor_Listener {
  protected $_field;

  /**
   * factory method
   */
  public static function construct($menu) {

    // check if a field with name 'menu_parent_node' exists.
    if (!($field = content_fields('field_menu_parent_node'))) {
      return NULL;
    }
    return new self($field);
  }

  /**
   * Protected constructor. Use factory method instead.
   */
  protected function __construct($field) {
    $this->_field = $field;
  }

  /**
   * Listener callback for submitted menu items
   * with router path 'node/%'
   */
  public function notifyItem__node__($item, $trail) {
    $m = array();
    if (!preg_match('/^node\\/(\\d+)$/', $item['link_path'], $m)) {

      // not a node..
      return;
    }
    $nid = (int) $m[1];

    // $node = db_fetch_object(db_query('SELECT nid, vid FROM node WHERE nid = %d', $nid));
    $node = node_load($nid);
    if (isset($node->field_menu_parent_node)) {
      $parent_nid = NULL;
      if (isset($item['plid'])) {
        $parent_item = $trail[$item['plid']];
        if (preg_match('/^node\\/(\\d+)$/', $parent_item['link_path'], $m)) {
          $parent_nid = (int) $m[1];
        }
      }
      if (!empty($parent_nid)) {
        $node->field_menu_parent_node = array(
          array(
            'nid' => $parent_nid,
          ),
        );
      }
      else {
        $node->field_menu_parent_node = array();
      }
      node_save($node);
    }
  }

}

Classes

Namesort descending Description
menu_editor_nodereference_class_menu_editor_Listener Implementation of class_menu_editor_Listener (which is a new type of hook, hehe)