Completed
Push — matrix_fields_fixes ( d87407...3179a4 )
by
unknown
06:02
created

MatrixBlockType::getBlockTypeByHandle()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 17
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 7
CRAP Score 1.0019

Importance

Changes 0
Metric Value
dl 0
loc 17
ccs 7
cts 8
cp 0.875
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 13
nc 1
nop 1
crap 1.0019
1
<?php
2
3
namespace NerdsAndCompany\Schematic\Converters\Models;
4
5
use Craft;
6
use craft\base\Model;
7
8
/**
9
 * Schematic Matrix Block Types Converter.
10
 *
11
 * Sync Craft Setups.
12
 *
13
 * @author    Nerds & Company
14
 * @copyright Copyright (c) 2015-2018, Nerds & Company
15
 * @license   MIT
16
 *
17
 * @see      http://www.nerds.company
18
 */
19
class MatrixBlockType extends Base
20
{
21
    /**
22
     * {@inheritdoc}
23
     */
24 1
    public function getRecordDefinition(Model $record): array
25
    {
26 1
        $definition = parent::getRecordDefinition($record);
27
28 1
        unset($definition['attributes']['fieldId']);
29 1
        unset($definition['attributes']['hasFieldErrors']);
30
31 1
        $definition['fields'] = Craft::$app->controller->module->modelMapper->export($record->fieldLayout->getFields());
32
33 1
        return $definition;
34
    }
35
36
    /**
37
     * {@inheritdoc}
38
     */
39 1
    public function saveRecord(Model $record, array $definition): bool
40
    {
41
        // Get existing fields by block type handle
42 1
        $existingFields = [];
43 1
        $existingBlockTypes = Craft::$app->matrix->getBlockTypesByFieldId($record->fieldId);
44
        foreach ($existingBlockTypes as $existingBlockType) {
45
            if ($existingBlockType->handle == $definition['attributes']['handle']) {
46
                $existingFields = $existingBlockType->getFields();
47
            }
48
        }
49
50
        // Set the content table for this matrix block
51
        $originalContentTable = Craft::$app->content->contentTable;
52
        $matrixField = Craft::$app->fields->getFieldById($record->fieldId);
53
        $contentTable = Craft::$app->matrix->getContentTableName($matrixField);
54
        Craft::$app->content->contentTable = $contentTable;
55
56
        // Get the matrix block fields from the definition
57
        $modelMapper = Craft::$app->controller->module->modelMapper;
58
        $fields = $modelMapper->import($definition['fields'], $existingFields, [], false);
59
        $record->setFields($fields);
0 ignored issues
show
Bug introduced by
The method setFields() does not exist on craft\base\Model. Did you maybe mean fields()?

This check marks calls to methods that do not seem to exist on an object.

This is most likely the result of a method being renamed without all references to it being renamed likewise.

Loading history...
60
61
        // Save the matrix block
62
        $result = Craft::$app->matrix->saveBlockType($record, false);
63
64
        // Restore the content table to what it was before
65
        Craft::$app->content->contentTable = $originalContentTable;
66
67
        return $result;
68
    }
69
70
    /**
71
     * {@inheritdoc}
72
     */
73 1
    public function deleteRecord(Model $record): bool
74
    {
75 1
        return Craft::$app->matrix->deleteBlockType($record);
76
    }
77
}
78