Exemple #1
0
 /**
  * @param array $config
  *
  * @return bool
  */
 protected function isDisabledItem(array $config)
 {
     $createOnly = isset($config['options']['create_only']) && $config['options']['create_only'];
     // disable config attribute if its value cannot be changed
     if ($createOnly && $this->configModel->getId()) {
         return true;
     }
     // disable field config attribute if its value cannot be changed for some field types
     // an attribute marked as create only should not be disabled on create field page
     if ($this->config->getId() instanceof FieldConfigId && !empty($config['options']['immutable_type']) && in_array($this->config->getId()->getFieldType(), $config['options']['immutable_type'], true) && (!$createOnly || $this->configModel->getId())) {
         return true;
     }
     return false;
 }
Exemple #2
0
 /**
  * Gets config id for the given model
  *
  * @param ConfigModel $model
  * @param string      $scope
  *
  * @return ConfigIdInterface
  */
 public function getConfigIdByModel($model, $scope)
 {
     if ($model instanceof FieldConfigModel) {
         return new FieldConfigId($scope, $model->getEntity()->getClassName(), $model->getFieldName(), $model->getType());
     } else {
         return new EntityConfigId($scope, $model->getClassName());
     }
 }
Exemple #3
0
 /**
  * {@inheritdoc}
  */
 public function buildForm(FormBuilderInterface $builder, array $options)
 {
     foreach ($this->items as $code => $config) {
         if (isset($config['form']['type'])) {
             $options = isset($config['form']['options']) ? $config['form']['options'] : array();
             $options['config_id'] = $this->config->getId();
             $options['config_is_new'] = $this->configModel->getId() == false;
             /**
              * Disable field on editAction
              */
             if (isset($config['options']['create_only']) && $this->configModel->getId()) {
                 $options['disabled'] = true;
                 $this->appendClassAttr($options, 'disabled-' . $config['form']['type']);
             }
             $propertyOnForm = false;
             $properties = [];
             if (isset($config['options']['required_property'])) {
                 $properties[] = $config['options']['required_property'];
             }
             if (isset($config['options']['required_properties'])) {
                 $properties = array_merge($properties, $config['options']['required_properties']);
             }
             if (!empty($properties)) {
                 foreach ($properties as $property) {
                     if (isset($property['config_id'])) {
                         $configId = $property['config_id'];
                         $fieldName = array_key_exists('field_name', $configId) ? $configId['field_name'] : false;
                         if ($fieldName === false && $this->config->getId() instanceof FieldConfigId) {
                             $fieldName = $this->config->getId()->getFieldName();
                         }
                         $className = isset($configId['class_name']) ? $configId['class_name'] : $this->config->getId()->getClassName();
                         $scope = isset($configId['scope']) ? $configId['scope'] : $this->config->getId()->getScope();
                         if ($fieldName) {
                             $configId = new FieldConfigId($scope, $className, $fieldName, $this->config->getId()->getFieldType());
                         } else {
                             $configId = new EntityConfigId($scope, $className);
                         }
                         //check if requirement property is set in this form
                         if ($className == $this->config->getId()->getClassName()) {
                             if ($fieldName) {
                                 if ($this->config->getId() instanceof FieldConfigId && $this->config->getId()->getFieldName() == $fieldName) {
                                     $propertyOnForm = true;
                                 }
                             } else {
                                 $propertyOnForm = true;
                             }
                         }
                     } else {
                         $propertyOnForm = true;
                         $configId = $this->config->getId();
                     }
                     $requireConfig = $this->configManager->getConfig($configId);
                     if ($requireConfig->get($property['code']) != $property['value']) {
                         if ($propertyOnForm) {
                             $this->appendClassAttr($options, 'hide');
                         } else {
                             continue;
                         }
                     }
                 }
                 if ($propertyOnForm) {
                     $this->setAttr($options, 'data-requireProperty', $configId->toString() . $property['code']);
                     $this->setAttr($options, 'data-requireValue', $property['value']);
                 }
             }
             if (isset($config['constraints'])) {
                 $options['constraints'] = $config['constraints'];
             }
             $this->setAttr($options, 'data-property_id', $this->config->getId()->toString() . $code);
             $builder->add($code, $config['form']['type'], $options);
         }
     }
 }
Exemple #4
0
 /**
  * @dataProvider modelProvider
  */
 public function testFromArray(ConfigModel $model)
 {
     $values = ['value1' => 1, 'value2' => 2, 'indexed1' => 3, 'indexed2' => 4];
     $indexed = ['indexed1' => true, 'indexed2' => true];
     $model->fromArray('test_scope', $values, $indexed);
     $this->assertEquals($values, $model->toArray('test_scope'));
     $indexedValues = $model->getIndexedValues();
     $expectedCount = $model instanceof EntityConfigModel ? 4 : 2;
     $this->assertCount($expectedCount, $indexedValues);
     $indexedValues = $indexedValues->toArray();
     /** @var ConfigModelIndexValue $indexedValue */
     if ($model instanceof EntityConfigModel) {
         $indexedValue = array_shift($indexedValues);
         $this->assertEquals('entity_config', $indexedValue->getScope());
         $this->assertEquals('module_name', $indexedValue->getCode());
         $this->assertEquals(self::TEST_MODULE, $indexedValue->getValue());
         $indexedValue = array_shift($indexedValues);
         $this->assertEquals('entity_config', $indexedValue->getScope());
         $this->assertEquals('entity_name', $indexedValue->getCode());
         $this->assertEquals(self::TEST_ENTITY, $indexedValue->getValue());
     }
     $indexedValue = array_shift($indexedValues);
     $this->assertEquals('test_scope', $indexedValue->getScope());
     $this->assertEquals('indexed1', $indexedValue->getCode());
     $this->assertEquals(3, $indexedValue->getValue());
     $indexedValue = array_shift($indexedValues);
     $this->assertEquals('test_scope', $indexedValue->getScope());
     $this->assertEquals('indexed2', $indexedValue->getCode());
     $this->assertEquals(4, $indexedValue->getValue());
     $values = ['value2' => 1, 'indexed2' => 2];
     $model->fromArray('test_scope', $values, $indexed);
     $this->assertEquals($values, $model->toArray('test_scope'));
     $indexedValues = $model->getIndexedValues();
     $expectedCount = $model instanceof EntityConfigModel ? 3 : 1;
     $this->assertCount($expectedCount, $indexedValues);
     $indexedValues = $indexedValues->toArray();
     if ($model instanceof EntityConfigModel) {
         $indexedValue = array_shift($indexedValues);
         $this->assertEquals('entity_config', $indexedValue->getScope());
         $this->assertEquals('module_name', $indexedValue->getCode());
         $this->assertEquals(self::TEST_MODULE, $indexedValue->getValue());
         $indexedValue = array_shift($indexedValues);
         $this->assertEquals('entity_config', $indexedValue->getScope());
         $this->assertEquals('entity_name', $indexedValue->getCode());
         $this->assertEquals(self::TEST_ENTITY, $indexedValue->getValue());
     }
     $indexedValue = array_shift($indexedValues);
     $this->assertEquals('test_scope', $indexedValue->getScope());
     $this->assertEquals('indexed2', $indexedValue->getCode());
     $this->assertEquals(2, $indexedValue->getValue());
     $values = [];
     $model->fromArray('test_scope', $values, $indexed);
     $this->assertEquals($values, $model->toArray('test_scope'));
 }