Welcome to the Question2Answer Q&A. There's also a demo if you just want to try it out.
+3 votes
586 views
in Q2A Core by
I wish the user were required to select a subcategory but so most users will leave the first sub-category

1 Answer

+4 votes
by
selected by
 
Best answer

I've been thinking on a relatively decent approach that would be flexible and that wouldn't require a core hack. I couldn't find one that fulfills both requirements. So I went for the one that doesn't require a core hack.

1. Edit the qa-theme/<your-theme>/qa-theme.php file

2. Locate the doctype() function. If it doesn't exist then just add it as explained in step 3

3. Change it so that it looks like this:

public function doctype() {
    if ($this->template === 'ask' && isset($this->content['form']['fields']['category']['options'])) {
        $categories = $this->content['form']['fields']['category']['options'];
        $categoryId = null;
        if (!isset($categoryId)) {
            foreach ($categories as $catId => $catName) {
                if (is_numeric($catId)) {
                    $categoryId = $catId;
                    break;
                }
            }
        }
        if (isset($categoryId)) {
            $categoryPath = qa_db_select_with_pending(qa_db_category_nav_selectspec($categoryId, true, false, true));
            $categoryPath = qa_category_path($categoryPath, $categoryId);
            $categoryPath = array_keys($categoryPath);
            $categoryPath = '/' . implode('/', $categoryPath);

            $templateLine = "    qa_category_select('category', %s);";
            $emptyLine = sprintf($templateLine, qa_js(''));
            $categoryLine = sprintf($templateLine, qa_js($categoryPath));
            foreach ($this->content['script'] as &$line) {
                if ($line === $emptyLine) {
                    $line = $categoryLine;
                    break;
                }
            }
        }
    }
    parent::doctype();
}

4. This will select the first category from top to bottom in the combobox. If you want to select a particular one, even if it is a child category, this does the magic too. You just need to input the appropriate category ID. For example, if you want to open category ID 5 then you should replace this line:

$categoryId = null;

with this one

$categoryId = 5;

In order to find out the category ID of a category just navigate to admin/categories. In there browse the categories until you find the one you want to default to. Check the URL in the location bar of your browser. It should look like this admin/categories?edit=X . The category ID is the number X.

...