Changes the block type.

@param Model\BlockInterface $block The block
@param string               $type  The block new type
@param array                $data  The block new data

@throws InvalidOperationException
172774
public function changeType(Model\BlockInterface $block, $type, array $data = [])
    {
        if ($type === $block->getType()) {
            return;
        }

        if ($block->isNamed()) {
            throw new InvalidOperationException(
                "The type of this block can't be changed."
            );
        }

        // Plugin removal
        $this->editor
            ->getBlockPlugin($block->getType())
            ->remove($block);

        // Sets the new type
        $block->setType($type);

        // Plugin creation
        $this->editor
            ->getBlockPlugin($block->getType())
            ->create($block, $data);
    }
1216330
public function create($contentOrName, $type = null, array $data = [])
    {
        // Check if container or name is defined
        if (!(
            $contentOrName instanceof Model\ContentInterface ||
            (is_string($contentOrName) && 0 < strlen($contentOrName))
        )
        ) {
            throw new InvalidOperationException("Excepted instance of ContentInterface or string.");
        }

        // Default type if null
        if (null === $type) {
            $type = $this->defaultType;
        }

        // New instance
        $container = $this->editor->getRepository()->createContainer();
        $container->setType($type);

        // Plugin creation
        $plugin = $this->editor->getContainerPlugin($type);
        $plugin->create($container, $data);

        // Create default row
        if (!$plugin instanceof CopyPlugin) {
            $this->editor->getRowManager()->create($container);
        }

        // Add to container if available
        if ($contentOrName instanceof Model\ContentInterface) {
            $count = $contentOrName->getContainers()->count();
            $container->setPosition($count);
            $contentOrName->addContainer($container);
        } else {
            $container->setName($contentOrName);
        }

        return $container;
    }
649654
public function revert(BlockInterface $block, $version)
    {
        $block->accept(new RevertVisitor($this, $version));

        if ($block instanceof BlockContainerInterface) {
            self::treeAddMissingChildren($block);
            self::treeRemoveInvalidChildren($block);
            $block->accept(new SortVisitor());
        }
    }
1219059
protected function createBlocks(BlockInterface $block)
    {
        $content = [];
        $content['id'] = $block->getId();
        $content['name'] = $block->getName();
        $content['enabled'] = $block->getEnabled();
        $content['position'] = $block->getPosition();
        $content['settings'] = $block->getSettings();
        $content['type'] = $block->getType();
        $content['created_at'] = $block->getCreatedAt()->format('U');
        $content['updated_at'] = $block->getUpdatedAt()->format('U');
        $content['blocks'] = [];

        foreach ($block->getChildren() as $child) {
            $content['blocks'][] = $this->createBlocks($child);
        }

        return $content;
    }
1274160
public function show(ReadBlockInterface $block)
    {
        $contentSearch = $block->getAttribute('contentSearch');

        if (!isset($contentSearch['contentId'])) {
            throw new \InvalidArgumentException();
        }

        $contentId = $contentSearch['contentId'];
        $language = $this->currentSiteManager->getSiteLanguage();
        $content = $this->contentRepository->findPublishedVersion($contentId, $language);

        if ($content) {
            $parameters = array(
                'class' => $block->getStyle(),
                'id' => $block->getId(),
                'content' => $content
            );

            return $this->render(
                'OpenOrchestraDisplayBundle:Block/ConfigurableContent:show.html.twig',
                $parameters
            );
        }

        throw new ContentNotFoundException($contentId);
    }
25622
public function saveBlockConfiguration(ServerRequestInterface $request, int $block_id): void
    {
        $this->setBlockSetting($block_id, 'type', $request->get('type', 'pedigree'));
        $this->setBlockSetting($block_id, 'pid', $request->get('pid', ''));
    }
855179
public function create($rowOrName, $type = null, array $data = [])
    {
        // Check if row or name is defined
        if (!(
            $rowOrName instanceof Model\RowInterface ||
            (is_string($rowOrName) && 0 < strlen($rowOrName))
        )
        ) {
            throw new InvalidOperationException("Excepted instance of RowInterface or string.");
        }

        // Default type if null
        if (null === $type) {
            $type = $this->defaultType;
        }

        // New instance
        $block = $this->editor->getRepository()->createBlock();
        $block->setType($type);

        // Layout creation
//        $this->editor->getLayoutAdapter()->createBlock($block, $data);

        // Plugin creation
        $this->editor->getBlockPlugin($type)->create($block, $data);

        // Add to row if available
        if ($rowOrName instanceof Model\RowInterface) {
            $count = $rowOrName->getBlocks()->count();
            $block
                ->setPosition($count);

            $rowOrName->addBlock($block);
        } else {
            $block->setName($rowOrName);
        }

        return $block;
    }
957454
public function validate(ErrorElement $errorElement, BlockInterface $block)
    {
        if (!$block->getId() && !$block->getType()) {
            return;
        }

        if ($this->inValidate) {
            return;
        }

        // As block can be nested, we only need to validate the main block, no the children
        try {
            $this->inValidate = true;
            $this->get($block)->validateBlock($errorElement, $block);
            $this->inValidate = false;
        } catch (\Exception $e) {
            $this->inValidate = false;
        }
    }
630911
private function validateChangeCollectionType(Block $block, string $collectionIdentifier, int $newType, string $queryType): void
    {
        $this->validate(
            $newType,
            [
                new Constraints\NotBlank(),
                new Constraints\Choice(
                    [
                        'choices' => [
                            Collection::TYPE_MANUAL,
                            Collection::TYPE_DYNAMIC,
                        ],
                        'strict' => true,
                    ]
                ),
            ],
            'new_type'
        );

        $blockDefinition = $block->getDefinition();
        if (!$blockDefinition->hasCollection($collectionIdentifier)) {
            return;
        }

        $collectionConfig = $blockDefinition->getCollection($collectionIdentifier);

        if ($newType === Collection::TYPE_DYNAMIC) {
            if (!$collectionConfig->isValidQueryType($queryType)) {
                throw ValidationException::validationFailed(
                    'new_type',
                    sprintf(
                        'Query type "%s" is not allowed in selected block.',
                        $queryType
                    )
                );
            }
        } elseif ($newType === Collection::TYPE_MANUAL) {
            if ($collectionConfig->getValidItemTypes() === []) {
                throw ValidationException::validationFailed(
                    'new_type',
                    'Selected block does not allow manual collections.'
                );
            }
        }
    }
1009849
protected function block(OutputBlock $block)
    {
        if (empty($block->lines) && empty($block->children)) {
            return;
        }

        $this->currentBlock = $block;

        $pre = $this->indentStr();

        if (! empty($block->selectors)) {
            $this->blockSelectors($block);

            $this->indentLevel++;
        }

        if (! empty($block->lines)) {
            $this->blockLines($block);
        }

        if (! empty($block->children)) {
            $this->blockChildren($block);
        }

        if (! empty($block->selectors)) {
            $this->indentLevel--;

            if (empty($block->children)) {
                $this->write($this->break);
            }

            $this->write($pre . $this->close . $this->break);
        }
    }
407796
private function resolve(BlockInterface $block, $settings)
    {
        $optionsResolver = new \Sonata\BlockBundle\Util\OptionsResolver();

        $this->configureSettings($optionsResolver, $block);

        $service = $this->blockService->get($block);

        // Caching method reflection
        // NEXT_MAJOR: Remove everything here
        $serviceClass = \get_class($service);
        if (!isset($this->reflectionCache[$serviceClass])) {
            $reflector = new \ReflectionMethod($service, 'setDefaultSettings');
            $isOldOverwritten = \get_class($service) === $reflector->getDeclaringClass()->getName();

            // Prevention for service classes implementing directly the interface and not extends the new base class
            if (!method_exists($service, 'configureSettings')) {
                $isNewOverwritten = false;
            } else {
                $reflector = new \ReflectionMethod($service, 'configureSettings');
                $isNewOverwritten = \get_class($service) === $reflector->getDeclaringClass()->getName();
            }

            $this->reflectionCache[$serviceClass] = [
                'isOldOverwritten' => $isOldOverwritten,
                'isNewOverwritten' => $isNewOverwritten,
            ];
        }

        // NEXT_MAJOR: Keep Only else case
        if ($this->reflectionCache[$serviceClass]['isOldOverwritten'] && !$this->reflectionCache[$serviceClass]['isNewOverwritten']) {
            @trigger_error(
                'The Sonata\BlockBundle\Block\BlockServiceInterface::setDefaultSettings() method is deprecated'
                .' since version 2.3 and will be removed in 4.0. Use configureSettings() instead.'
                .' This method will be added to the BlockServiceInterface with SonataBlockBundle 4.0.',
                E_USER_DEPRECATED
            );
            $service->setDefaultSettings($optionsResolver);
        } else {
            $service->configureSettings($optionsResolver);
        }

        return $optionsResolver->resolve($settings);
    }
1163920
private function updateBlockTranslations(Block $block, array $translationsData): void
    {
        $mainLocale = $block->getMainLocale();

        foreach ($block->getAvailableLocales() as $locale) {
            if ($locale === $mainLocale) {
                continue;
            }

            if (!array_key_exists($locale, $translationsData)) {
                throw new RuntimeException(
                    sprintf('Could not find locale "%s" in the given block data', $locale)
                );
            }

            $this->updateBlockTranslation($block, $translationsData[$locale], $locale);
        }
    }
1329911
private function startBlock($type, $start, $value = NULL)
    {
        $this->_pos ++;
        $this->_current = $type;
        $this->_blocks[$this->_pos] = array($type, $start, $start, $value);
        return $this;
    }
998070
public function begin($name, $type = 'replace')
    {
        $stacks = [$name, $type];
        if (!isset($this->opens[$name])) {
            $stacks[] = $this->placeholder($name);
        } elseif ($this->opens[$name]) {
            throw new BlockException($name, 'is opened');
        }

        $this->opens[$name] = true;
        $this->stacks[] = $stacks;
        ob_start();
    }
107258
private function resolveType(BlockTypeInterface $type)
    {
        $typeExtensions = [];
        $parentType = $type->getParent();
        $fqcn = \get_class($type);

        foreach ($this->extensions as $extension) {
            $typeExtensions = array_merge(
                $typeExtensions,
                $extension->getTypeExtensions($fqcn)
            );
        }

        return $this->resolvedTypeFactory->createResolvedType(
            $type,
            $typeExtensions,
            $parentType ? $this->getType($parentType) : null
        );
    }
479185
public function removeInvalidBlock(Block $block, $reason = 'unknown')
	{
		/* @var $testBlock Entity\Abstraction\Block */
		foreach ($this as $index => $testBlock) {
			if ($testBlock->equals($block)) {
				\Log::warn("Block {$block} removed as invalid with reason: {$reason}");
				
				$this->offsetUnset($index);
			}
		}
	}
97187
public function setType($type)
    {
      if(!is_string($type) || strlen(trim($type)) == 0)
        throw new InvalidArgumentException(__METHOD__ . '; Invalid type, must be a non empty string');

      $this->type = $type;
    }