Skip to content

Commit

Permalink
Support overwite block types on child theme
Browse files Browse the repository at this point in the history
  • Loading branch information
mostafasoufi committed Mar 25, 2021
1 parent c5232ef commit 48c23f1
Show file tree
Hide file tree
Showing 2 changed files with 63 additions and 2 deletions.
48 changes: 46 additions & 2 deletions src/Blocks.php
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,16 @@ class Blocks
*/
protected $building = false;

/**
* @var mixed|null
*/
private $nameSpace;

/**
* @var mixed|null
*/
private $childNameSpace;

/**
* ContentBuilder constructor
*/
Expand All @@ -64,6 +74,10 @@ public function init($config = [])
// Parse block types from given config
$blockTypes = $this->parseConfig($config);

// Set parent & child namespaces
$this->nameSpace = isset($config['namespace']) ? $config['namespace'] : null;
$this->childNameSpace = isset($config['child_namespace']) ? $config['child_namespace'] : null;

// Register blocks from config, if applicable
$this->registerBlockTypes($blockTypes);
}
Expand All @@ -86,10 +100,20 @@ protected function parseConfig($config)
if (array_key_exists('namespace', $config) && array_key_exists('blocktypes', $config)) {

// Add trailing slash if it's missing
$namespace = rtrim($config['namespace'], '\\') . '\\';
$namespace = rtrim($config['namespace'], '\\') . '\\';
$childNamespace = rtrim($config['child_namespace'], '\\') . '\\';

foreach ($config['blocktypes'] as &$blockType) {
if (is_string($blockType)) {

/**
* Overwrite child block type if exists
*/
if (class_exists($childNamespace . $blockType)) {
$blockType = $childNamespace . $blockType;
continue;
}

$blockType = $namespace . $blockType;
}
}
Expand Down Expand Up @@ -321,7 +345,7 @@ public function getBuilder($postId = null, $blockName = null)

if (!isset($this->builders[$postId])) {
if (!$this->building || $postId === 'option') {
$this->building = true;
$this->building = true;
$this->builders[$postId] = $this->createContentBuilder($postId);
} else {
if (!$blockName) {
Expand All @@ -338,6 +362,26 @@ public function getBuilder($postId = null, $blockName = null)
return $this->builders[$postId];
}

/**
* Get namespace
*
* @return mixed|null
*/
public function getNameSpace()
{
return $this->nameSpace;
}

/**
* Get child namespace
*
* @return mixed|null
*/
public function getChildNameSpace()
{
return $this->childNameSpace;
}

/**
* @param $postId
*
Expand Down
17 changes: 17 additions & 0 deletions src/FlexibleContentBlockType.php
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,24 @@ protected function setupFlexibleContent()
*/
public function registerBlockType($blockType)
{
/**
* Generating the child namespace
*/
$overwrittenNameSpace = str_replace(
Blocks::getInstance()->getNameSpace(),
Blocks::getInstance()->getChildNameSpace(),
$blockType
);

if (is_string($blockType)) {

/**
* Overwrite the child block type if exists
*/
if (class_exists($overwrittenNameSpace)) {
$blockType = $overwrittenNameSpace;
}

$blockType = new $blockType();
}

Expand Down

0 comments on commit 48c23f1

Please sign in to comment.