CMB2::object_id PHP Method

object_id() public method

Get object id from global space if no id is provided
Since: 1.0.0
public object_id ( integer $object_id ) : integer
$object_id integer Object ID
return integer $object_id Object ID
    public function object_id($object_id = 0)
    {
        global $pagenow;
        if ($object_id) {
            $this->object_id = $object_id;
            return $this->object_id;
        }
        if ($this->object_id) {
            return $this->object_id;
        }
        // Try to get our object ID from the global space
        switch ($this->object_type()) {
            case 'user':
                $object_id = isset($_REQUEST['user_id']) ? $_REQUEST['user_id'] : $object_id;
                $object_id = !$object_id && 'user-new.php' != $pagenow && isset($GLOBALS['user_ID']) ? $GLOBALS['user_ID'] : $object_id;
                break;
            case 'comment':
                $object_id = isset($_REQUEST['c']) ? $_REQUEST['c'] : $object_id;
                $object_id = !$object_id && isset($GLOBALS['comments']->comment_ID) ? $GLOBALS['comments']->comment_ID : $object_id;
                break;
            case 'term':
                $object_id = isset($_REQUEST['tag_ID']) ? $_REQUEST['tag_ID'] : $object_id;
                break;
            default:
                $object_id = isset($GLOBALS['post']->ID) ? $GLOBALS['post']->ID : $object_id;
                $object_id = isset($_REQUEST['post']) ? $_REQUEST['post'] : $object_id;
                break;
        }
        // reset to id or 0
        $this->object_id = $object_id ? $object_id : 0;
        return $this->object_id;
    }

Usage Example

 /**
  * Displays form markup
  * @since  0.1.3
  * @param  int  $term_id Term ID
  */
 public function display_form($term_id)
 {
     if (!class_exists('CMB2')) {
         return;
     }
     $this->do_override_filters($term_id);
     $object_id = $this->id($term_id);
     // Hard-code object ID
     $this->cmb->object_id($object_id);
     // Hard-code object type
     $this->cmb->object_type('options-page');
     // Enqueue JS/CSS
     if ($this->cmb->prop('cmb_styles')) {
         CMB2_hookup::enqueue_cmb_css();
     }
     if ($this->cmb->prop('enqueue_js')) {
         CMB2_hookup::enqueue_cmb_js();
     }
     // Add object id to the form for easy access
     printf('<input type="hidden" name="term_opt_name" value="%s">', $object_id);
     printf('<input type="hidden" name="object_id" value="%s">', $object_id);
     // Show cmb form
     $this->cmb->show_form();
 }