This function create the default category if it not exists.
426693
def blog__create_default_category
      category_parent = LatoBlog::CategoryParent.find_by(meta_default: true)
      return if category_parent

      category_parent = LatoBlog::CategoryParent.new(meta_default: true)
      throw 'Impossible to create default category parent' unless category_parent.save

      languages = blog__get_languages_identifier
      languages.each do |language|
        category = LatoBlog::Category.new(
          title: 'Default',
          meta_permalink: "default_#{language}",
          meta_language: language,
          lato_core_superuser_creator_id: 1,
          lato_blog_category_parent_id: category_parent.id
        )
        throw 'Impossible to create default category' unless category.save
      end
    end
1028547
public function actionCreate()
    {
        $model = new Categories;

        if ($model->load(Yii::$app->request->post()) && $model->save()) {
            if (Yii::$app->request->isAjax) 
            {
                header('Content-type: application/json');
                echo Json::encode(['status'=>'DONE','model'=>$model]);
                exit();
            }
            else
            {
                return $this->redirect(['view', 'id' => $model->id]);
            }
        }
        return $this->render('create', [
            'model' => $model,
        ]);
    }
760261
public static function categoryParentSingleSelectCreate($categories) {

        // STEP 1: Initiatize the html select tag
        $html = "";
        $html .= '<select name="parent_id" id="parent_id" size="6" class="form-control" >';

        $html .= '<option ';
        $html .= 'value="';
        $html .= 0;
        $html .= '">';
        $html .= 'No Parent Category';
        $html .= '</option>"';

        // STEP 2: Construct the <option></option> categories for ALL categories in the categories table
        foreach ($categories as $category) {
            $html .= '<option ';
            $html .= 'value="';
            $html .= $category->id;
            $html .= '">';
            $html .= $category->title;
            $html .= '</option>"';
        }
        $html .= '</select>';

        $html .= self::renderBootstrapMultiselectPlugin(count($categories));

        return $html;
    }
240059
protected function submitAddCategory()
    {
        $this->setSubmitted(null, $this->getParam());
        $this->validateComponent('category');
        $this->addCategory();
    }
1343223
private function get_default_category_id() {
        global $CFG;

        if ($this->defaultcategoryid === null) {
            $category = core_course_category::get_default();
            $this->defaultcategoryid = $category->id;
        }

        return $this->defaultcategoryid;
    }
257784
def create_child_item_for_category(category)
      category.child_items.find_or_create_by(categorizable_type: self.categorizable.class.base_class.name, categorizable_id: self.categorizable.id, master_item_id: self.id)
    end
822886
def set_default(self):
        
        queryset = self.get_default_queryset()
        if queryset.exists():
            queryset.update(default=False)
386740
protected function setupDefaults()
    {
        $defaults = (isset($this->config['defaults'])) ?
            $this->config['defaults'] :
            [];

        foreach ($defaults as $key => $value) {
            if ($key == 'images') {
                if (empty($this->images)) {
                    $this->images = $value;
                }
            } elseif ($key == 'url' && $value === null) {
                $this->setUrl(app('url')->current());
            } elseif (! empty($value) && ! array_key_exists($key, $this->properties)) {
                $this->addProperty($key, $value);
            }
        }
    }
645219
public function create()
    {
        /**
         * @var Category $category
         */
        $classNamespace = $this->getEntityNamespace();
        $category = new $classNamespace();
        $category
            ->setSubcategories(new ArrayCollection())
            ->setRoot(true)
            ->setPosition(0)
            ->setEnabled(true)
            ->setCreatedAt($this->now());

        return $category;
    }
1056226
protected function magic_get_category() {
        $this->ensure_category_loaded();
        if (!empty($this->_categories)) {
            return reset($this->_categories);
        } else {
            return null;
        }
    }
836712
def category(self, categories):
        
        if isinstance(categories, list):
            for c in categories:
                self.add_category(c)
        else:
            self.add_category(categories)
116637
public function createCategory($categoryKey)
    {
        $this->requireCategoryDoesNotExist($categoryKey);

        $this->validateName($categoryKey);

        $this->optionCategoryModel->create(['key' => $categoryKey]);
        $this->refresh();
        $this->options[$categoryKey] = [];
    }
275869
def registerCategory(category):
    
    # parse what level it is set to based on _DEBUG
    # example: *:2,admin:4
    global _DEBUG
    global _levels
    global _categories

    level = 0
    chunks = _DEBUG.split(',')
    for chunk in chunks:
        if not chunk:
            continue
        if ':' in chunk:
            spec, value = chunk.split(':')
        else:
            spec = '*'
            value = chunk

        # our glob is unix filename style globbing, so cheat with fnmatch
        # fnmatch.fnmatch didn't work for this, so don't use it
        if category in fnmatch.filter((category, ), spec):
            # we have a match, so set level based on string or int
            if not value:
                continue
            try:
                level = int(value)
            except ValueError: # e.g. *; we default to most
                level = 5
    # store it
    _categories[category] = level
629300
public static function get_default() {
        if ($visiblechildren = self::top()->get_children()) {
            $defcategory = reset($visiblechildren);
        } else {
            $toplevelcategories = self::get_tree(0);
            $defcategoryid = $toplevelcategories[0];
            $defcategory = self::get($defcategoryid, MUST_EXIST, true);
        }
        return $defcategory;
    }
1346020
def add_category
      category_name = self.name.to_s.split('.').first
      # if one word then add to general category
      category_name = self.name.to_s.split('.').size == 1 ? 'general' : self.name.to_s.split('.').first
      self.category = TranslationCenter::Category.where(name: category_name).first_or_create
      self.last_accessed = Time.now
    end
212214
def setdefault(self, key, default=None):
        """"""
        identity = self._title(key)
        for i, k, v in self._impl._items:
            if i == identity:
                return v
        self.add(key, default)
        return default
346358
def add_category(self, category):
        '''
        
        '''
        if category == sre.CATEGORY_DIGIT:
            self._categories |= UNICODE_DIGIT_CATEGORIES
        elif category == sre.CATEGORY_NOT_DIGIT:
            self._categories |= UNICODE_CATEGORIES - UNICODE_DIGIT_CATEGORIES
        elif category == sre.CATEGORY_SPACE:
            self._categories |= UNICODE_SPACE_CATEGORIES
            for c in (UNICODE_SPACE_CHARS if self._unicode else SPACE_CHARS):
                self._whitelist_chars.add(c)
        elif category == sre.CATEGORY_NOT_SPACE:
            self._categories |= UNICODE_CATEGORIES - UNICODE_SPACE_CATEGORIES
            for c in (UNICODE_SPACE_CHARS if self._unicode else SPACE_CHARS):
                self._blacklist_chars.add(c)
        elif category == sre.CATEGORY_WORD:
            self._categories |= UNICODE_WORD_CATEGORIES
            self._whitelist_chars.add(u'_')
            if HAS_WEIRD_WORD_CHARS and self._unicode:
                for c in UNICODE_WEIRD_NONWORD_CHARS:
                    self._blacklist_chars.add(c)
        elif category == sre.CATEGORY_NOT_WORD:
            self._categories |= UNICODE_CATEGORIES - UNICODE_WORD_CATEGORIES
            self._blacklist_chars.add(u'_')
            if HAS_WEIRD_WORD_CHARS and self._unicode:
                for c in UNICODE_WEIRD_NONWORD_CHARS:
                    self._whitelist_chars.add(c)