6
6
namespace Magento \Framework \View \Element ;
7
7
8
8
use Magento \Framework \ObjectManagerInterface ;
9
+ use Magento \Framework \ObjectManager \ConfigInterface ;
10
+ use Magento \Framework \App \ObjectManager ;
9
11
10
12
/**
11
13
* Creates Blocks
16
18
class BlockFactory
17
19
{
18
20
/**
19
- * Object manager
20
- *
21
21
* @var ObjectManagerInterface
22
22
*/
23
23
protected $ objectManager ;
24
24
25
+ /**
26
+ * Object manager config
27
+ *
28
+ * @var ConfigInterface
29
+ */
30
+ private $ config ;
31
+
25
32
/**
26
33
* Constructor
27
34
*
28
35
* @param ObjectManagerInterface $objectManager
36
+ * @param ConfigInterface $config
29
37
*/
30
- public function __construct (ObjectManagerInterface $ objectManager )
31
- {
38
+ public function __construct (
39
+ ObjectManagerInterface $ objectManager ,
40
+ ?ConfigInterface $ config = null
41
+ ) {
32
42
$ this ->objectManager = $ objectManager ;
43
+ $ this ->config = $ config ?: ObjectManager::getInstance ()->get (ConfigInterface::class);
33
44
}
34
45
35
46
/**
@@ -42,8 +53,9 @@ public function __construct(ObjectManagerInterface $objectManager)
42
53
*/
43
54
public function createBlock ($ blockName , array $ arguments = [])
44
55
{
45
- $ blockName = ltrim ($ blockName , '\\' );
46
- $ block = $ this ->objectManager ->create ($ blockName , $ arguments );
56
+ $ blockName = ltrim ($ blockName , '\\' );
57
+ $ arguments = $ this ->getConfigArguments ($ blockName , $ arguments );
58
+ $ block = $ this ->objectManager ->create ($ blockName , $ arguments );
47
59
if (!$ block instanceof BlockInterface) {
48
60
throw new \LogicException ($ blockName . ' does not implement BlockInterface ' );
49
61
}
@@ -52,4 +64,24 @@ public function createBlock($blockName, array $arguments = [])
52
64
}
53
65
return $ block ;
54
66
}
67
+
68
+ /**
69
+ * Get All Config Arguments based on Block Name
70
+ *
71
+ * @param string $blockName
72
+ * @param array $arguments
73
+ * @return array $arguments
74
+ */
75
+ private function getConfigArguments ($ blockName , array $ arguments = [])
76
+ {
77
+ if (!$ this ->config ) {
78
+ return $ arguments ;
79
+ }
80
+ $ configArguments = $ this ->config ->getArguments ($ blockName );
81
+ if ($ configArguments && isset ($ configArguments ['data ' ])) {
82
+ $ arguments ['data ' ] = ($ arguments && isset ($ arguments ['data ' ])) ?
83
+ array_merge ($ arguments ['data ' ], $ configArguments ['data ' ]) : $ configArguments ['data ' ];
84
+ }
85
+ return $ arguments ;
86
+ }
55
87
}
0 commit comments