Example #1
0
 /**
  * Get JS config
  *
  * @return array
  */
 public function getJsConfig()
 {
     if (isset($this->wrappedComponent)) {
         return array_replace_recursive((array) $this->wrappedComponent->getData('config'), (array) $this->getData('config'));
     }
     return (array) $this->getData('config');
 }
 public function testApplyEditing()
 {
     $this->column->expects($this->once())->method('getConfiguration')->willReturn(['dataType' => 'text', 'visible' => true]);
     $this->validationRules->expects($this->once())->method('getValidationRules')->with(true, [$this->validationRule])->willReturn(['validate-email' => true, 'required-entry' => true]);
     $this->column->expects($this->once())->method('setData')->with('config', ['dataType' => 'text', 'visible' => true, 'editor' => ['editorType' => 'text', 'validation' => ['validate-email' => true, 'required-entry' => true]]]);
     $this->component->applyEditing($this->column, 'text', [$this->validationRule], true);
 }
Example #3
0
 /**
  * Call prepare method in the component UI
  *
  * @param UiComponentInterface $component
  * @return void
  */
 protected function prepareComponent(UiComponentInterface $component)
 {
     foreach ($component->getChildComponents() as $child) {
         $this->prepareComponent($child);
     }
     $component->prepare();
 }
Example #4
0
 /**
  * Add children data
  *
  * @param array $topNode
  * @param UiComponentInterface $component
  * @param string $componentType
  * @return void
  */
 protected function addChildren(array &$topNode, UiComponentInterface $component, $componentType)
 {
     $childrenNode = [];
     $childComponents = $component->getChildComponents();
     if (!empty($childComponents)) {
         /** @var UiComponentInterface $child */
         foreach ($childComponents as $child) {
             if ($child instanceof DataSourceInterface) {
                 continue;
             }
             self::addChildren($childrenNode, $child, $child->getComponentName());
         }
     }
     /** @var JsConfigInterface $component */
     $config = $component->getJsConfig();
     if (is_string($config)) {
         $topNode[] = $config;
     } else {
         $nodeData = ['type' => $componentType, 'name' => $component->getName()];
         if (!empty($childrenNode)) {
             $nodeData['children'] = $childrenNode;
         }
         if (isset($config['dataScope'])) {
             $nodeData['dataScope'] = $config['dataScope'];
             unset($config['dataScope']);
         }
         if (!empty($config)) {
             $nodeData['config'] = $config;
         }
         $topNode[] = $nodeData;
     }
 }
Example #5
0
 /**
  * Build component structure and retrieve
  *
  * @param UiComponentInterface $component
  * @return array
  */
 public function generate(UiComponentInterface $component)
 {
     /** @var LayoutInterface $layout */
     if (!($layoutDefinition = $component->getData('layout'))) {
         $layoutDefinition = ['type' => 'generic'];
     }
     $layout = $this->layoutPool->create($layoutDefinition['type'], $layoutDefinition);
     return $layout->build($component);
 }
 /**
  * Returns columns list
  *
  * @param UiComponentInterface $component
  * @return UiComponentInterface[]
  */
 protected function getColumns(UiComponentInterface $component)
 {
     if (!isset($this->columns[$component->getName()])) {
         $columns = $this->getColumnsComponent($component);
         foreach ($columns->getChildComponents() as $column) {
             $this->columns[$component->getName()][$column->getName()] = $column;
         }
     }
     return $this->columns[$component->getName()];
 }
Example #7
0
 /**
  * Call prepare method in the component UI
  *
  * @param UiComponentInterface $component
  * @return void
  */
 protected function prepareComponent(UiComponentInterface $component)
 {
     $childComponents = $component->getChildComponents();
     if (!empty($childComponents)) {
         foreach ($childComponents as $child) {
             $this->prepareComponent($child);
         }
     }
     $component->prepare();
 }
Example #8
0
 /**
  * Returns columns list
  *
  * @param UiComponentInterface $component
  * @return UiComponentInterface[]
  */
 protected function getColumns(UiComponentInterface $component)
 {
     if (!isset($this->columns[$component->getName()])) {
         $columns = $this->getColumnsComponent($component);
         foreach ($columns->getChildComponents() as $column) {
             if ($column->getData('config/label') && $column->getData('config/dataType') !== 'actions') {
                 $this->columns[$component->getName()][$column->getName()] = $column;
             }
         }
     }
     return $this->columns[$component->getName()];
 }
Example #9
0
 /**
  * Render block HTML
  *
  * @return string
  */
 protected function _toHtml()
 {
     foreach ($this->getChildNames() as $childName) {
         $childBlock = $this->getLayout()->getBlock($childName);
         if ($childBlock) {
             $wrapper = $this->blockWrapperFactory->create(['block' => $childBlock, 'data' => ['name' => 'block_' . $childName]]);
             $this->component->addComponent('block_' . $childName, $wrapper);
         }
     }
     $result = $this->component->render();
     return (string) $result;
 }
 public function testAttachAndNotify()
 {
     $type = 'test_type';
     $this->component->expects($this->any())->method('getComponentName')->willReturn($type);
     $this->observer->expects($this->any())->method('update')->with($this->component);
     /** @var UiComponentInterface $component2 */
     $component2 = $this->getMockBuilder('Magento\\Framework\\View\\Element\\UiComponentInterface')->getMockForAbstractClass();
     $component2->expects($this->any())->method('getComponentName')->willReturn('other_type');
     $this->processor->register($this->component);
     $this->processor->register($component2);
     $this->processor->attach($type, $this->observer);
     $this->processor->notify($type);
 }
Example #11
0
 /**
  * Render data
  *
  * @param UiComponentInterface $component
  * @param string $template
  * @return string
  * @throws \Exception
  * @SuppressWarnings(PHPMD.UnusedFormalParameter)
  */
 public function render(UiComponentInterface $component, $template = '')
 {
     $context = $component->getContext();
     $isComponent = $context->getRequestParam('componentJson');
     if ($isComponent) {
         $data = $this->structure->generate($component);
         return $this->encoder->encode($data);
     } else {
         $data = $component->getContext()->getDataSourceData($component);
         $data = reset($data);
         return $this->encoder->encode($data['config']['data']);
     }
 }
Example #12
0
 /**
  * Compiles the Element node
  *
  * @param Compiler $compiler
  * @param \DOMElement $node
  * @param UiComponentInterface $component
  * @param Object $context
  * @return void
  */
 public function compile(Compiler $compiler, \DOMElement $node, UiComponentInterface $component, Object $context)
 {
     $name = $node->getAttribute('name');
     $content = (string) $component->renderChildComponent($name);
     $name .= '_' . sprintf('%x', crc32(spl_object_hash($context)));
     if (!empty($content)) {
         $compiler->setPostprocessingData($name, $content);
         $newNode = $node->ownerDocument->createTextNode(Compiler::PATTERN_TAG . $name . Compiler::PATTERN_TAG);
         $node->parentNode->replaceChild($newNode, $node);
     } else {
         $node->parentNode->removeChild($node);
     }
 }
Example #13
0
 /**
  * {@inheritdoc}
  */
 public function update(UiComponentInterface $component)
 {
     if (!$component instanceof \Magento\Ui\Component\Filters) {
         return;
     }
     $attributeCodes = $component->getContext()->getRequestParam('attributes_codes');
     if ($attributeCodes) {
         foreach ($this->getAttributes($attributeCodes) as $attribute) {
             $filter = $this->filterFactory->create($attribute, $component->getContext());
             $filter->prepare();
             $component->addComponent($attribute->getAttributeCode(), $filter);
         }
     }
 }
Example #14
0
 /**
  * Prepare component configuration
  *
  * @return void
  */
 public function prepare()
 {
     $dataType = $this->getData('config/dataType');
     if ($dataType) {
         $this->wrappedComponent = $this->uiComponentFactory->create($this->getName(), $dataType, array_merge(['context' => $this->getContext()], (array) $this->getData()));
         $this->wrappedComponent->prepare();
         $wrappedComponentConfig = $this->getJsConfig($this->wrappedComponent);
         // Merge JS configuration with wrapped component configuration
         $jsConfig = array_replace_recursive($wrappedComponentConfig, $this->getJsConfig($this));
         $this->setData('js_config', $jsConfig);
         $this->setData('config', array_replace_recursive((array) $this->wrappedComponent->getData('config'), (array) $this->getData('config')));
     }
     $this->applySorting();
     parent::prepare();
 }
Example #15
0
 /**
  * Compiles the Element node
  *
  * @param Compiler $compiler
  * @param \DOMElement $node
  * @param UiComponentInterface $component
  * @param Object $context
  * @return void
  */
 public function compile(Compiler $compiler, \DOMElement $node, UiComponentInterface $component, Object $context)
 {
     $result = $component->renderChildComponent($node->getAttribute('name'));
     if ($result instanceof Result) {
         $node->parentNode->replaceChild($result->getDocumentElement(), $node);
     } else {
         if (!empty($result) && is_scalar($result)) {
             $newFragment = $node->ownerDocument->createDocumentFragment();
             $newFragment->appendXML($result);
             $node->parentNode->replaceChild($newFragment, $node);
             $node->parentNode->removeChild($node);
         } else {
             $node->parentNode->removeChild($node);
         }
     }
 }
Example #16
0
 /**
  * @param UiComponentInterface $view
  * @return string
  */
 protected function getDataXml(UiComponentInterface $view)
 {
     $result = ['configuration' => $view->getRenderContext()->getStorage()->getComponentsData($view->getName())->getData(), 'data' => []];
     foreach ($view->getRenderContext()->getStorage()->getData($view->getName()) as $key => $value) {
         if (is_object($value)) {
             if (method_exists($value, 'toXml')) {
                 $result['data'][$key] = $value->toXml();
             } else {
                 $result['data'][$key] = $this->objectToXml($value);
             }
         } else {
             $result['data'][$key] = $value;
         }
     }
     return $this->generator->arrayToXml($result);
 }
Example #17
0
 /**
  * Prepare component configuration
  *
  * @return void
  * @throws \Magento\Framework\Exception\LocalizedException
  */
 public function prepare()
 {
     $formElement = $this->getData('config/formElement');
     if (null === $formElement) {
         throw new LocalizedException(__('The configuration parameter "formElement" is a required for "' . $this->getName() . '" field.'));
     }
     // Create of wrapped component
     $this->wrappedComponent = $this->uiComponentFactory->create($this->getName(), $formElement, array_merge(['context' => $this->getContext()], (array) $this->getData()));
     $this->wrappedComponent->prepare();
     // Merge JS configuration with wrapped component configuration
     $wrappedComponentConfig = $this->getJsConfig($this->wrappedComponent);
     $jsConfig = array_replace_recursive($wrappedComponentConfig, $this->getJsConfig($this));
     $jsConfig['extends'] = $this->wrappedComponent->getComponentName();
     $this->setData('js_config', $jsConfig);
     $this->setData('config', array_replace_recursive((array) $this->wrappedComponent->getData('config'), (array) $this->getData('config')));
     parent::prepare();
 }
Example #18
0
 /**
  * @inheritDoc
  */
 public function update(UiComponentInterface $component)
 {
     if ($component instanceof ColumnInterface) {
         $filterType = $component->getData('config/filter');
         if (is_array($filterType)) {
             $filterType = $filterType['filterType'];
         }
         if (!$filterType) {
             return;
         }
         if (isset($this->filterMap[$filterType])) {
             $filterComponent = $this->uiComponentFactory->create($component->getName(), $this->filterMap[$filterType], ['context' => $this->getContext()]);
             $filterComponent->setData('config', $component->getConfiguration());
             $filterComponent->prepare();
             $this->addComponent($component->getName(), $filterComponent);
         }
     }
 }
 /**
  * Add editor config
  *
  * @param UiComponentInterface $column
  * @param string $frontendInput
  * @param array $validationRules
  * @param bool|false $isRequired
  * @return UiComponentInterface
  */
 public function applyEditing(UiComponentInterface $column, $frontendInput, array $validationRules, $isRequired = false)
 {
     if (in_array($frontendInput, $this->editableFields)) {
         $config = $column->getConfiguration();
         $editorType = $config['dataType'];
         if (isset($config['editor']) && is_string($config['editor'])) {
             $editorType = $config['editor'];
         }
         if (!(isset($config['editor']) && isset($config['editor']['editorType']))) {
             $config['editor'] = ['editorType' => $editorType];
         }
         $validationRules = $this->validationRules->getValidationRules($isRequired, $validationRules);
         if (!empty($config['editor']['validation'])) {
             $validationRules = array_merge($config['editor']['validation'], $validationRules);
         }
         $config['editor']['validation'] = $validationRules;
         $column->setData('config', $config);
     }
     return $column;
 }
Example #20
0
 /**
  * Add navigation block
  *
  * @return void
  */
 protected function addNavigationBlock()
 {
     $pageLayout = $this->component->getContext()->getPageLayout();
     /** @var \Magento\Ui\Component\Layout\Tabs\Nav $navBlock */
     if (isset($this->navContainerName)) {
         $navBlock = $pageLayout->addBlock('Magento\\Ui\\Component\\Layout\\Tabs\\Nav', 'tabs_nav', $this->navContainerName);
     } else {
         $navBlock = $pageLayout->addBlock('Magento\\Ui\\Component\\Layout\\Tabs\\Nav', 'tabs_nav', 'content');
     }
     $navBlock->setTemplate('Magento_Ui::layout/tabs/nav/default.phtml');
     $navBlock->setData('data_scope', $this->namespace);
     $this->component->getContext()->addComponentDefinition('nav', ['component' => 'Magento_Ui/js/form/components/tab_group', 'config' => ['template' => 'ui/tab'], 'extends' => $this->namespace]);
 }
 /**
  * Update component data
  *
  * @param array $componentData
  * @param UiComponentInterface $component
  * @return $this
  */
 protected function updateComponent(array $componentData, UiComponentInterface $component)
 {
     $config = $component->getData('config');
     // XML data configuration override configuration coming from the DB
     $config = array_replace_recursive($componentData, $config);
     $component->setData('config', $config);
     return $this;
 }
 /**
  * Create button container
  *
  * @param string $key
  * @param UiComponentInterface $view
  * @return Container
  */
 protected function createContainer($key, UiComponentInterface $view)
 {
     $container = $this->context->getPageLayout()->createBlock('Magento\\Ui\\Component\\Control\\Container', 'container-' . $view->getName() . '-' . $key, ['data' => ['button_item' => $this->items[$key], 'context' => $view]]);
     return $container;
 }
 /**
  * Create child of form
  *
  * @param UiComponentInterface $childComponent
  * @param string $name
  * @return UiComponentInterface
  * @throws \Magento\Framework\Exception\LocalizedException
  */
 protected function createChildFormComponent(UiComponentInterface $childComponent, $name)
 {
     $panelComponent = $this->uiComponentFactory->create($name, $this->getConfig(self::CONFIG_PANEL_COMPONENT), ['context' => $this->component->getContext(), 'components' => [$childComponent->getName() => $childComponent]]);
     $panelComponent->prepare();
     $this->component->addComponent($name, $panelComponent);
     return $panelComponent;
 }
Example #24
0
 /**
  * Add options to component
  *
  * @param UiComponentInterface $component
  * @param array $attributeData
  * @return void
  */
 public function addOptions(UiComponentInterface $component, array $attributeData)
 {
     $config = $component->getData('config');
     if (count($attributeData[AttributeMetadata::OPTIONS]) && !isset($config[AttributeMetadata::OPTIONS])) {
         $component->setData('config', array_merge($config, [AttributeMetadata::OPTIONS => $attributeData[AttributeMetadata::OPTIONS]]));
     }
 }
Example #25
0
 /**
  * Get JS configuration
  *
  * @param UiComponentInterface $component
  * @param null|string $extends
  * @return array
  */
 protected function getConfiguration(UiComponentInterface $component, $extends = null)
 {
     $jsConfig = (array) $component->getData('js_config');
     if (isset($jsConfig['extends'])) {
         return $jsConfig;
     } else {
         if (null !== $extends) {
             $jsConfig['extends'] = $extends;
         } else {
             $jsConfig['extends'] = $component->getContext()->getNamespace();
         }
     }
     return $jsConfig;
 }
Example #26
0
 /**
  * Update field data
  *
  * @param array $fieldData
  * @param UiComponentInterface $component
  * @return void
  */
 protected function updateField(array $fieldData, UiComponentInterface $component)
 {
     $config = $component->getData('config');
     // XML data configuration override configuration coming from the DB
     $config = array_replace_recursive($fieldData, $config);
     $config = $this->updateDataScope($config, $component->getName());
     $component->setData('config', $config);
 }
 /**
  * Get configuration of related JavaScript Component
  * (force extending the root component if component does not extend other component)
  *
  * @param UiComponentInterface $component
  * @return array
  */
 public function getJsConfig(UiComponentInterface $component)
 {
     $jsConfig = (array) $component->getData('js_config');
     if (!isset($jsConfig['extends'])) {
         $jsConfig['extends'] = $component->getContext()->getNamespace();
     }
     return $jsConfig;
 }
Example #28
0
 /**
  * Render data
  *
  * @param UiComponentInterface $view
  * @param string $template
  * @return string
  */
 public function render(UiComponentInterface $view, $template = '')
 {
     return $view->getRenderContext()->getConfigBuilder()->toJson($view->getRenderContext()->getStorage(), $view->getName());
 }
Example #29
0
 /**
  * To prepare the structure of child components
  *
  * @param UiComponentInterface $component
  * @param string $parentName
  * @return array
  */
 protected function prepareChildComponents(UiComponentInterface $component, $parentName)
 {
     $name = $component->getName();
     $childComponents = $component->getChildComponents();
     $childrenStructure = [];
     foreach ($childComponents as $childName => $child) {
         $isVisible = $child->getData('config/visible');
         if ($isVisible !== null && $isVisible == 0) {
             continue;
         }
         /**
          * @var UiComponentInterface $childComponent
          * @var array $childStructure
          */
         list($childComponent, $childStructure) = $this->prepareChildComponents($child, $component->getName());
         $childrenStructure = array_merge($childrenStructure, $childStructure);
         $component->addComponent($childName, $childComponent);
     }
     $structure = [$name => ['type' => $component->getComponentName(), 'name' => $component->getName(), 'children' => $childrenStructure]];
     list($config, $dataScope) = $this->prepareConfig((array) $component->getConfiguration(), $name, $parentName);
     if ($dataScope !== false) {
         $structure[$name]['dataScope'] = $dataScope;
     }
     $structure[$name]['config'] = $config;
     return [$component, $structure];
 }
Example #30
0
 /**
  * Call `prepareData` method of all the components
  *
  * @param array $data
  * @param UiComponentInterface $component
  * @return void
  */
 protected function prepareDataSource(array &$data, UiComponentInterface $component)
 {
     $childComponents = $component->getChildComponents();
     if (!empty($childComponents)) {
         foreach ($childComponents as $child) {
             $this->prepareDataSource($data, $child);
         }
     }
     $data = $component->prepareDataSource($data);
 }