Skip to content

Commit 8fe90d0

Browse files
committed
Merge branch 'release2.11.0'
2 parents 7b9327b + 73e99ee commit 8fe90d0

File tree

5 files changed

+99
-6
lines changed

5 files changed

+99
-6
lines changed

src/Block.php

+5-5
Original file line numberDiff line numberDiff line change
@@ -124,14 +124,15 @@ public function page() {
124124
}
125125

126126
/**
127-
* Returns the first matching view, passing it the fields
127+
* Returns the first matching view, passing it the Block and optional data
128128
*
129+
* @param array $with
129130
* @return View
130131
* @throws UnableToRenderException
131132
*/
132-
public function render() {
133+
public function render($with = []) {
133134
try {
134-
return view()->first($this->views(), ['block' => $this]);
135+
return view()->first($this->views(), array_merge(['block' => $this], $with));
135136
} catch (\Exception $exception) {
136137
throw new UnableToRenderException('None of the views in the given array exist.', $this);
137138
}
@@ -266,8 +267,7 @@ private function processFields() {
266267
* @throws \Storyblok\ApiException
267268
*/
268269
private function getFieldType($field, $key) {
269-
$factory = new FieldFactory();
270-
return $factory->build($this, $field, $key);
270+
return (new FieldFactory())->build($this, $field, $key);
271271
}
272272

273273
/**

src/Field.php

+52
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,15 @@ abstract class Field
2121
*/
2222
protected $block;
2323

24+
25+
/**
26+
* Key/value pairs of additional content you want the
27+
* field to have access to. Pass anything you like
28+
*
29+
* @var array
30+
*/
31+
public $with;
32+
2433
/**
2534
* Creates the new field taking it’s content and a reference
2635
* to the parent Block
@@ -38,18 +47,56 @@ public function __construct($content, $block)
3847
}
3948
}
4049

50+
/**
51+
* Returns the content of the Field
52+
*
53+
* @return array|string
54+
*/
4155
public function content() {
4256
return $this->content;
4357
}
4458

59+
/**
60+
* Returns the Block this Field belongs to
61+
*
62+
* @return Block
63+
*/
4564
public function block() {
4665
return $this->block;
4766
}
4867

68+
/**
69+
* Checks if the requested key is in the Field’s content
70+
*
71+
* @param $key
72+
* @return bool
73+
*/
4974
public function has($key) {
5075
return array_key_exists($key, $this->content);
5176
}
5277

78+
79+
/**
80+
* Allows key/value pairs to be passed into the Field such as CSS
81+
* classes when rendering __toString or another content.
82+
* Example: {{ $field->with(['classes' => 'my-class']) }}
83+
*
84+
* @param $with
85+
* @return Field
86+
*/
87+
public function with($with) {
88+
$this->with = $with;
89+
90+
return $this;
91+
}
92+
93+
/**
94+
* Magic accessor to pull content from the content. Works just like
95+
* Laravel’s model accessors.
96+
*
97+
* @param $key
98+
* @return false|mixed|string
99+
*/
53100
public function __get($key) {
54101
$accessor = 'get' . Str::studly($key) . 'Attribute';
55102

@@ -68,5 +115,10 @@ public function __get($key) {
68115
}
69116
}
70117

118+
/**
119+
* Prints the Field as a string
120+
*
121+
* @return string
122+
*/
71123
abstract public function __toString();
72124
}

src/Folder.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ abstract class Folder
4141
/**
4242
* @var int number of items to return
4343
*/
44-
protected $perPage = 10;
44+
protected $perPage;
4545

4646

4747
/**

tests/FieldTest.php

+22
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
use Riclep\Storyblok\Tests\Fixtures\Fields\AssetWithAccessor;
2121
use Riclep\Storyblok\Tests\Fixtures\Fields\HeroImage;
2222
use Riclep\Storyblok\Tests\Fixtures\Fields\Imgix;
23+
use Riclep\Storyblok\Tests\Fixtures\Fields\WithImage;
2324

2425
class FieldTest extends TestCase
2526
{
@@ -764,4 +765,25 @@ public function can_set_focal_point()
764765
$this->assertEquals('right bottom', $field4->focalPointAlignment('center', true));
765766
}
766767

768+
769+
/** @test */
770+
public function can_add_with_content()
771+
{
772+
// default
773+
$field = new WithImage(json_decode(
774+
'{
775+
"id": 1223942,
776+
"alt": null,
777+
"name": "",
778+
"focus": "",
779+
"title": null,
780+
"filename": "https://a.storyblok.com/f/96945/100x100/c55f649622/2020-ar-fusionacusoft-letterbox-2.jpg",
781+
"copyright": null,
782+
"fieldtype": "asset"
783+
}', true
784+
), null);
785+
786+
$this->assertEquals('<img src="https://a.storyblok.com/f/96945/100x100/c55f649622/2020-ar-fusionacusoft-letterbox-2.jpg class="some-class">', (string) $field->with(['class' => 'some-class']));
787+
}
788+
767789
}

tests/Fixtures/Fields/WithImage.php

+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
<?php
2+
3+
4+
namespace Riclep\Storyblok\Tests\Fixtures\Fields;
5+
6+
7+
use Riclep\Storyblok\Fields\Image;
8+
9+
class WithImage extends Image
10+
{
11+
public function __toString()
12+
{
13+
if ($this->content['filename']) {
14+
return '<img src="' . $this->content['filename'] . ' class="' . $this->with['class'] . '">';
15+
}
16+
17+
return '';
18+
}
19+
}

0 commit comments

Comments
 (0)