Skip to content

Commit 843cf62

Browse files
authored
feat: using blade engine correctly (#96)
1 parent 2ebd7df commit 843cf62

File tree

4 files changed

+77
-31
lines changed

4 files changed

+77
-31
lines changed

modularity-form-builder.php

+7-1
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,13 @@
77
* Author: Kristoffer Svanmark, Sebastian Thulin
88
*/
99

10+
use ModularityFormBuilder\Blade\Blade;
11+
use ComponentLibrary\Init as ComponentLibraryInit;
12+
1013
define('FORM_BUILDER_MODULE_PATH', plugin_dir_path(__FILE__));
1114
define('FORM_BUILDER_MODULE_URL', plugins_url('', __FILE__));
1215
define('FORM_BUILDER_MODULE_TEMPLATE_PATH', FORM_BUILDER_MODULE_PATH . 'templates/');
16+
define('FORM_BUILDER_MODULE_VIEW_PATH', FORM_BUILDER_MODULE_PATH . 'source/php/Module/views');
1317

1418
//Load lang
1519
load_plugin_textdomain('modularity-form-builder', false, plugin_basename(dirname(__FILE__)) . '/languages');
@@ -19,6 +23,8 @@
1923
require_once FORM_BUILDER_MODULE_PATH . 'vendor/autoload.php';
2024
}
2125

26+
$bladeInstance = new Blade(new ComponentLibraryInit([]));
27+
2228
// Acf auto import and export
2329
add_action('plugins_loaded', function () {
2430
$acfExportManager = new \AcfExportManager\AcfExportManager();
@@ -32,5 +38,5 @@
3238

3339
// Start application
3440
if (function_exists('get_field')) {
35-
new ModularityFormBuilder\App();
41+
new ModularityFormBuilder\App($bladeInstance);
3642
}

source/php/App.php

+5-1
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,13 @@
22

33
namespace ModularityFormBuilder;
44

5+
use ModularityFormBuilder\Blade\Blade;
6+
57
class App
68
{
79
public $postType = 'mod-form';
810

9-
public function __construct()
11+
public function __construct(private Blade $bladeInstance)
1012
{
1113
new Submission();
1214
new Options();
@@ -41,6 +43,7 @@ public function registerPostTypes()
4143
{
4244
// Default form submission post type
4345
new Entity\PostType(
46+
$this->bladeInstance,
4447
'form-submissions',
4548
__('Form submission', 'modularity-form-builder'),
4649
__('Form submissions', 'modularity-form-builder')
@@ -77,6 +80,7 @@ public function registerPostTypes()
7780
unset($postTypeArr['cap']);
7881

7982
new Entity\PostType(
83+
$this->bladeInstance,
8084
$postTypeArr['name'],
8185
$postTypeArr['labels']['singular_name'] ?? $postTypeArr['label'],
8286
$postTypeArr['label'],

source/php/Blade/Blade.php

+44
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
<?php
2+
3+
namespace ModularityFormBuilder\Blade;
4+
5+
use ComponentLibrary\Init as ComponentLibraryInit;
6+
use HelsingborgStad\BladeService\BladeServiceInterface;
7+
8+
class Blade
9+
{
10+
private BladeServiceInterface $bladeEngine;
11+
12+
public function __construct(private ComponentLibraryInit $componentLibrary)
13+
{
14+
$this->bladeEngine = $this->componentLibrary->getEngine();
15+
}
16+
17+
public function render($view, $data = [], $compress = true, $viewPaths = [FORM_BUILDER_MODULE_VIEW_PATH])
18+
{
19+
$markup = '';
20+
$data = array_merge($data, array('errorMessage' => false));
21+
22+
try {
23+
$markup = $this->bladeEngine->makeView($view, $data, [], $viewPaths)->render();
24+
} catch (\Throwable $e) {
25+
$this->bladeEngine->errorHandler($e)->print();
26+
}
27+
28+
if ($compress == true) {
29+
$replacements = array(
30+
["~<!--(.*?)-->~s", ""],
31+
["/\r|\n/", ""],
32+
["!\s+!", " "]
33+
);
34+
35+
foreach ($replacements as $replacement) {
36+
$markup = preg_replace($replacement[0], $replacement[1], $markup);
37+
}
38+
39+
return $markup;
40+
}
41+
42+
return $markup;
43+
}
44+
}

source/php/Entity/PostType.php

+21-29
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
namespace ModularityFormBuilder\Entity;
44

5+
use ModularityFormBuilder\Blade\Blade;
56
use ModularityFormBuilder\Helper\NestedFields;
67

78
class PostType
@@ -11,7 +12,7 @@ class PostType
1112
public $namePlural;
1213
public $args;
1314

14-
public function __construct($postTypeSlug, $nameSingular, $namePlural, $args = array())
15+
public function __construct(private Blade $bladeInstance, $postTypeSlug, $nameSingular, $namePlural, $args = array())
1516
{
1617
$this->postTypeSlug = $postTypeSlug;
1718
$this->nameSingular = $nameSingular;
@@ -295,10 +296,8 @@ public function formdataDisplay()
295296
if (!$this->isGrantedUser($data['module_id'])) {
296297
//Error message
297298
$this->renderBlade(
298-
'unauthorized.blade.php',
299-
array(
300-
FORM_BUILDER_MODULE_PATH . 'source/php/Module/views/admin'
301-
),
299+
'unauthorized',
300+
'/admin',
302301
array(
303302
'title' => __("Access denied", 'modularity-form-builder'),
304303
'message' => __("You don't have the sufficient permissions to view this post.", 'modularity-form-builder'),
@@ -328,10 +327,8 @@ public function formdataDisplay()
328327
if (is_admin() && isset($fields['editable_back_end']) && $fields['editable_back_end'] == true) {
329328
//Editable
330329
$this->renderBlade(
331-
'form-edit.blade.php',
332-
array(
333-
FORM_BUILDER_MODULE_PATH . 'source/php/Module/views'
334-
),
330+
'form-edit',
331+
'',
335332
$data
336333
);
337334
} elseif (self::editableFrontend($post)) {
@@ -348,28 +345,22 @@ public function formdataDisplay()
348345

349346
//Static
350347
$this->renderBlade(
351-
'form-data.blade.php',
352-
array(
353-
FORM_BUILDER_MODULE_PATH . 'source/php/Module/views/admin'
354-
),
348+
'form-data',
349+
'/admin',
355350
$data
356351
);
357352

358353
//Editable
359354
$this->renderBlade(
360-
'form-edit-front.blade.php',
361-
array(
362-
FORM_BUILDER_MODULE_PATH . 'source/php/Module/views'
363-
),
355+
'form-edit-front',
356+
"",
364357
$data
365358
);
366359
} else {
367360
//Static
368361
$this->renderBlade(
369-
'form-data.blade.php',
370-
array(
371-
FORM_BUILDER_MODULE_PATH . 'source/php/Module/views/admin'
372-
),
362+
'form-data',
363+
'/admin',
373364
$data
374365
);
375366
}
@@ -529,15 +520,16 @@ private static function findMatchingNestedIndataArrayValue(&$nestedIndataArray,
529520
* @param array $path Array with file paths
530521
* @param array $data Template data
531522
*/
532-
public function renderBlade($fileName, $path, $data = array())
523+
public function renderBlade(string $viewName, string $path = '', $data = array())
533524
{
534-
add_filter('Municipio/blade/view_paths', array($this, 'addViewPaths'), 2, 1);
535-
$template = new \Municipio\template();
536-
$view = \Municipio\Helper\Template::locateTemplate($fileName, $path);
537-
$view = $template->cleanViewPath($fileName);
538-
if (function_exists('render_blade_view')) {
539-
echo render_blade_view($view, $data);
540-
}
525+
$html = $this->bladeInstance->render(
526+
$viewName,
527+
$data,
528+
true,
529+
[FORM_BUILDER_MODULE_VIEW_PATH . $path]
530+
);
531+
532+
echo $html;
541533
}
542534

543535
/**

0 commit comments

Comments
 (0)