NerdsAndCompany\Schematic\Services\TagGroups::import PHP Метод

import() публичный Метод

Attempt to import tagGroups.
public import ( array $tagGroupDefinitions, boolean $force = false ) : Result
$tagGroupDefinitions array
$force boolean If set to true tagGroups not included in the import will be deleted
Результат Result
    public function import(array $tagGroupDefinitions, $force = false)
    {
        Craft::log(Craft::t('Importing TagGroups'));
        $tagGroups = Craft::app()->tags->getAllTagGroups('handle');
        foreach ($tagGroupDefinitions as $tagGroupHandle => $tagGroupDefinition) {
            $tagGroup = array_key_exists($tagGroupHandle, $tagGroups) ? $tagGroups[$tagGroupHandle] : new TagGroupModel();
            unset($tagGroups[$tagGroupHandle]);
            $this->populateTagGroup($tagGroup, $tagGroupDefinition, $tagGroupHandle);
            if (!Craft::app()->tags->saveTagGroup($tagGroup)) {
                // Save taggroup via craft
                $this->addErrors($tagGroup->getAllErrors());
                continue;
            }
        }
        if ($force) {
            foreach ($tagGroups as $tagGroup) {
                Craft::app()->tags->deleteTagGroupById($tagGroup->id);
            }
        }
        return $this->getResultModel();
    }

Usage Example

Пример #1
0
 /**
  * @covers ::import
  * @dataProvider provideValidTagGroupDefinitions
  *
  * @param array $groupDefinitions
  */
 public function testImportWithForceOption(array $groupDefinitions)
 {
     $this->setMockTagsService();
     $this->setMockDbConnection();
     $this->setMockSchematicFields();
     $schematicUserGroupsService = new TagGroups();
     $import = $schematicUserGroupsService->import($groupDefinitions, true);
     $this->assertInstanceOf(Result::class, $import);
     $this->assertFalse($import->hasErrors());
 }