Field.php 22 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127
  1. <?php
  2. namespace Dcat\Admin\Form;
  3. use Dcat\Admin\Admin;
  4. use Dcat\Admin\Form;
  5. use Dcat\Admin\Widgets\Form as WidgetForm;
  6. use Illuminate\Contracts\Support\Arrayable;
  7. use Illuminate\Contracts\Support\Renderable;
  8. use Illuminate\Support\Arr;
  9. use Illuminate\Support\Fluent;
  10. use Illuminate\Support\Str;
  11. use Illuminate\Support\Traits\Macroable;
  12. /**
  13. * Class Field.
  14. */
  15. class Field implements Renderable
  16. {
  17. use Macroable, Form\Concerns\HasFieldValidator;
  18. const FILE_DELETE_FLAG = '_file_del_';
  19. /**
  20. * Element id.
  21. *
  22. * @var array|string
  23. */
  24. protected $id;
  25. /**
  26. * Element value.
  27. *
  28. * @var mixed
  29. */
  30. protected $value;
  31. /**
  32. * Data of all original columns of value.
  33. *
  34. * @var mixed
  35. */
  36. protected $data;
  37. /**
  38. * Field original value.
  39. *
  40. * @var mixed
  41. */
  42. protected $original;
  43. /**
  44. * Field default value.
  45. *
  46. * @var mixed
  47. */
  48. protected $default;
  49. /**
  50. * Element label.
  51. *
  52. * @var string
  53. */
  54. protected $label = '';
  55. /**
  56. * Column name.
  57. *
  58. * @var string|array
  59. */
  60. protected $column = '';
  61. /**
  62. * Form element name.
  63. *
  64. * @var string
  65. */
  66. protected $elementName = [];
  67. /**
  68. * Form element classes.
  69. *
  70. * @var array
  71. */
  72. protected $elementClass = [];
  73. /**
  74. * Variables of elements.
  75. *
  76. * @var array
  77. */
  78. protected $variables = [];
  79. /**
  80. * Options for specify elements.
  81. *
  82. * @var array
  83. */
  84. protected $options = [];
  85. /**
  86. * Checked for specify elements.
  87. *
  88. * @var array
  89. */
  90. protected $checked = [];
  91. /**
  92. * Css required by this field.
  93. *
  94. * @var array
  95. */
  96. protected static $css = [];
  97. /**
  98. * Js required by this field.
  99. *
  100. * @var array
  101. */
  102. protected static $js = [];
  103. /**
  104. * Script for field.
  105. *
  106. * @var string
  107. */
  108. protected $script = '';
  109. /**
  110. * Element attributes.
  111. *
  112. * @var array
  113. */
  114. protected $attributes = [];
  115. /**
  116. * Parent form.
  117. *
  118. * @var Form|WidgetForm
  119. */
  120. protected $form = null;
  121. /**
  122. * View for field to render.
  123. *
  124. * @var string
  125. */
  126. protected $view = '';
  127. /**
  128. * Help block.
  129. *
  130. * @var array
  131. */
  132. protected $help = [];
  133. /**
  134. * Key for errors.
  135. *
  136. * @var mixed
  137. */
  138. protected $errorKey;
  139. /**
  140. * Placeholder for this field.
  141. *
  142. * @var string|array
  143. */
  144. protected $placeholder;
  145. /**
  146. * Width for label and field.
  147. *
  148. * @var array
  149. */
  150. protected $width = [
  151. 'label' => 2,
  152. 'field' => 8,
  153. ];
  154. /**
  155. * If the form horizontal layout.
  156. *
  157. * @var bool
  158. */
  159. protected $horizontal = true;
  160. /**
  161. * column data format.
  162. *
  163. * @var \Closure
  164. */
  165. protected $customFormat = null;
  166. /**
  167. * @var bool
  168. */
  169. protected $display = true;
  170. /**
  171. * @var array
  172. */
  173. protected $labelClass = [];
  174. /**
  175. * @var \Closure
  176. */
  177. protected $prepareCallback;
  178. /**
  179. * Field constructor.
  180. *
  181. * @param string|array $column
  182. * @param array $arguments
  183. */
  184. public function __construct($column, $arguments = [])
  185. {
  186. $this->column = $column;
  187. $this->label = $this->formatLabel($arguments);
  188. $this->id = $this->formatId($column);
  189. }
  190. /**
  191. * Get the field element id.
  192. *
  193. * @return string|array
  194. */
  195. public function elementId()
  196. {
  197. return $this->id;
  198. }
  199. /**
  200. * Format the field element id.
  201. *
  202. * @param string|array $column
  203. *
  204. * @return string|array
  205. */
  206. protected function formatId($column)
  207. {
  208. $random = Str::random(5);
  209. if (is_array($column)) {
  210. $id = [];
  211. foreach (str_replace('.', '-', $column) as $k => $v) {
  212. $id[$k] = "{$v}-{$random}";
  213. }
  214. return $id;
  215. }
  216. return 'form-field-'.str_replace('.', '-', $column).'-'.$random;
  217. }
  218. /**
  219. * Format the label value.
  220. *
  221. * @param array $arguments
  222. *
  223. * @return string
  224. */
  225. protected function formatLabel($arguments = [])
  226. {
  227. $column = is_array($this->column) ? current($this->column) : $this->column;
  228. $label = isset($arguments[0]) ? $arguments[0] : ucfirst(admin_trans_field($column));
  229. return str_replace(['.', '_'], ' ', $label);
  230. }
  231. /**
  232. * Format the name of the field.
  233. *
  234. * @param string $column
  235. *
  236. * @return array|mixed|string
  237. */
  238. protected function formatName($column)
  239. {
  240. if (is_string($column)) {
  241. $name = explode('.', $column);
  242. if (count($name) == 1) {
  243. return $name[0];
  244. }
  245. $html = array_shift($name);
  246. foreach ($name as $piece) {
  247. $html .= "[$piece]";
  248. }
  249. return $html;
  250. }
  251. if (is_array($this->column)) {
  252. $names = [];
  253. foreach ($this->column as $key => $name) {
  254. $names[$key] = $this->formatName($name);
  255. }
  256. return $names;
  257. }
  258. return '';
  259. }
  260. /**
  261. * Set form element name.
  262. *
  263. * @param string $name
  264. *
  265. * @return $this|string
  266. *
  267. * @author Edwin Hui
  268. */
  269. public function elementName($name = null)
  270. {
  271. if ($name === null) {
  272. return $this->elementName ?: $this->formatName($this->column);
  273. }
  274. $this->elementName = $name;
  275. return $this;
  276. }
  277. /**
  278. * Fill data to the field.
  279. *
  280. * @param array $data
  281. *
  282. * @return void
  283. */
  284. final public function fill($data)
  285. {
  286. $this->data($data);
  287. $this->value = $this->formatFieldData($data);
  288. $this->callCustomFormatter();
  289. }
  290. /**
  291. * Format field data.
  292. *
  293. * @param array $data
  294. *
  295. * @return mixed
  296. */
  297. protected function formatFieldData($data)
  298. {
  299. if (is_array($this->column)) {
  300. $value = [];
  301. foreach ($this->column as $key => $column) {
  302. $value[$key] = Arr::get($data, $column);
  303. }
  304. return $value;
  305. }
  306. return Arr::get($data, $this->column, $this->value);
  307. }
  308. /**
  309. * custom format form column data when edit.
  310. *
  311. * @param \Closure $call
  312. *
  313. * @return $this
  314. */
  315. public function customFormat(\Closure $call)
  316. {
  317. $this->customFormat = $call;
  318. return $this;
  319. }
  320. /**
  321. * Set original value to the field.
  322. *
  323. * @param array $data
  324. *
  325. * @return void
  326. */
  327. final public function setOriginal($data)
  328. {
  329. $this->original = $this->formatFieldData($data);
  330. $this->callCustomFormatter('original', new Fluent($data));
  331. }
  332. /**
  333. * @param string $key
  334. * @param Fluent|null $dataremoveField
  335. */
  336. protected function callCustomFormatter($key = 'value', Fluent $data = null)
  337. {
  338. if ($this->customFormat) {
  339. $this->{$key} = $this->customFormat
  340. ->call(
  341. $data ?: $this->data(),
  342. $this->{$key},
  343. $this->column,
  344. $this
  345. );
  346. }
  347. }
  348. /**
  349. * @param Form|WidgetForm $form
  350. *
  351. * @return $this
  352. */
  353. public function setForm($form = null)
  354. {
  355. $this->form = $form;
  356. return $this;
  357. }
  358. /**
  359. * @return Fluent
  360. */
  361. public function values()
  362. {
  363. return $this->form ? $this->form->model() : new Fluent();
  364. }
  365. /**
  366. * Set width for field and label.
  367. *
  368. * @param int $field
  369. * @param int $label
  370. *
  371. * @return $this
  372. */
  373. public function width($field = 8, $label = 2)
  374. {
  375. $this->width = [
  376. 'label' => $label,
  377. 'field' => $field,
  378. ];
  379. return $this;
  380. }
  381. /**
  382. * Set the field options.
  383. *
  384. * @param array $options
  385. *
  386. * @return $this
  387. */
  388. public function options($options = [])
  389. {
  390. if ($options instanceof Arrayable) {
  391. $options = $options->toArray();
  392. }
  393. if (is_array($this->options)) {
  394. $this->options = array_merge($this->options, $options);
  395. } else {
  396. $this->options = $options;
  397. }
  398. return $this;
  399. }
  400. /**
  401. * Set the field option checked.
  402. *
  403. * @param array $checked
  404. *
  405. * @return $this
  406. */
  407. public function checked($checked = [])
  408. {
  409. if ($checked instanceof Arrayable) {
  410. $checked = $checked->toArray();
  411. }
  412. $this->checked = array_merge($this->checked, (array) $checked);
  413. return $this;
  414. }
  415. /**
  416. * Get or set key for error message.
  417. *
  418. * @param string $key
  419. *
  420. * @return $this|string
  421. */
  422. public function errorKey($key = null)
  423. {
  424. if ($key === null) {
  425. return $this->errorKey ?: $this->column;
  426. }
  427. $this->errorKey = $key;
  428. return $this;
  429. }
  430. /**
  431. * Set or get value of the field.
  432. *
  433. * @param null $value
  434. *
  435. * @return mixed
  436. */
  437. public function value($value = null)
  438. {
  439. if (is_null($value)) {
  440. return is_null($this->value) ? $this->default() : $this->value;
  441. }
  442. $this->value = $value;
  443. return $this;
  444. }
  445. /**
  446. * Set or get data.
  447. *
  448. * @param array $data
  449. *
  450. * @return $this|Fluent
  451. */
  452. public function data(array $data = null)
  453. {
  454. if (is_null($data)) {
  455. return $this->data ?: ($this->data = new Fluent());
  456. }
  457. $this->data = new Fluent($data);
  458. return $this;
  459. }
  460. /**
  461. * Get or set default value for field.
  462. *
  463. * @param $default
  464. *
  465. * @return $this
  466. */
  467. public function default($default = null)
  468. {
  469. if ($default === null) {
  470. if ($this->default instanceof \Closure) {
  471. return call_user_func($this->default, $this->form);
  472. }
  473. return $this->default;
  474. }
  475. $this->default = $default;
  476. return $this;
  477. }
  478. /**
  479. * Set help block for current field.
  480. *
  481. * @param string $text
  482. * @param string $icon
  483. *
  484. * @return $this
  485. */
  486. public function help($text = '', $icon = 'fa-info-circle')
  487. {
  488. $this->help = compact('text', 'icon');
  489. return $this;
  490. }
  491. /**
  492. * Get column of the field.
  493. *
  494. * @return string|array
  495. */
  496. public function column()
  497. {
  498. return $this->column;
  499. }
  500. /**
  501. * Get or set label of the field.
  502. *
  503. * @param null $label
  504. *
  505. * @return $this|string
  506. */
  507. public function label($label = null)
  508. {
  509. if ($label == null) {
  510. return $this->label;
  511. }
  512. if ($label instanceof \Closure) {
  513. $label = $label($this->label);
  514. }
  515. $this->label = $label;
  516. return $this;
  517. }
  518. public function old()
  519. {
  520. return old($this->column, $this->value());
  521. }
  522. /**
  523. * Get original value of the field.
  524. *
  525. * @return mixed
  526. */
  527. public function original()
  528. {
  529. return $this->original;
  530. }
  531. /**
  532. * Sanitize input data.
  533. *
  534. * @param array $input
  535. * @param string $column
  536. *
  537. * @return array
  538. */
  539. protected function sanitizeInput($input, $column)
  540. {
  541. if ($this instanceof Field\MultipleSelect) {
  542. $value = Arr::get($input, $column);
  543. Arr::set($input, $column, array_filter($value));
  544. }
  545. return $input;
  546. }
  547. /**
  548. * Add html attributes to elements.
  549. *
  550. * @param array|string $attribute
  551. * @param mixed $value
  552. *
  553. * @return $this
  554. */
  555. public function attribute($attribute, $value = null)
  556. {
  557. if (is_array($attribute)) {
  558. $this->attributes = array_merge($this->attributes, $attribute);
  559. } else {
  560. $this->attributes[$attribute] = (string) $value;
  561. }
  562. return $this;
  563. }
  564. /**
  565. * Specifies a regular expression against which to validate the value of the input.
  566. *
  567. * @param string $error
  568. * @param string $regexp
  569. *
  570. * @return $this
  571. */
  572. public function pattern($regexp, $error = null)
  573. {
  574. if ($error) {
  575. $this->attribute('data-pattern-error', $error);
  576. }
  577. return $this->attribute('pattern', $regexp);
  578. }
  579. /**
  580. * set the input filed required.
  581. *
  582. * @param bool $isLabelAsterisked
  583. *
  584. * @return $this
  585. */
  586. public function required($isLabelAsterisked = true)
  587. {
  588. if ($isLabelAsterisked) {
  589. $this->labelClass(['asterisk']);
  590. }
  591. $this->rules('required');
  592. return $this->attribute('required', true);
  593. }
  594. /**
  595. * Set the field automatically get focus.
  596. *
  597. * @return $this
  598. */
  599. public function autofocus()
  600. {
  601. return $this->attribute('autofocus', true);
  602. }
  603. /**
  604. * Set the field as readonly mode.
  605. *
  606. * @return $this
  607. */
  608. public function readOnly()
  609. {
  610. return $this->attribute('readonly', true);
  611. }
  612. /**
  613. * Set field as disabled.
  614. *
  615. * @return $this
  616. */
  617. public function disable()
  618. {
  619. return $this->attribute('disabled', true);
  620. }
  621. /**
  622. * Get or set field placeholder.
  623. *
  624. * @param string $placeholder
  625. *
  626. * @return $this|string
  627. */
  628. public function placeholder($placeholder = null)
  629. {
  630. if ($placeholder === null) {
  631. return $this->placeholder ?: trans('admin.input').' '.$this->label;
  632. }
  633. $this->placeholder = $placeholder;
  634. return $this;
  635. }
  636. /**
  637. * Prepare for a field value before update or insert.
  638. *
  639. * @param mixed $value
  640. *
  641. * @return mixed
  642. */
  643. protected function prepareToSave($value)
  644. {
  645. return $value;
  646. }
  647. /**
  648. * @param \Closure $closure
  649. *
  650. * @return $this
  651. */
  652. public function saving(\Closure $closure)
  653. {
  654. $this->prepareCallback = $closure;
  655. return $this;
  656. }
  657. /**
  658. * Prepare for a field value before update or insert.
  659. *
  660. * @param mixed $value
  661. *
  662. * @return mixed
  663. */
  664. final public function prepare($value)
  665. {
  666. $value = $this->prepareToSave($value);
  667. if ($handler = $this->prepareCallback) {
  668. $handler->bindTo($this->data);
  669. return $handler($value);
  670. }
  671. return $value;
  672. }
  673. /**
  674. * Format the field attributes.
  675. *
  676. * @return string
  677. */
  678. protected function formatAttributes()
  679. {
  680. $html = [];
  681. foreach ($this->attributes as $name => $value) {
  682. $html[] = $name.'="'.e($value).'"';
  683. }
  684. return implode(' ', $html);
  685. }
  686. /**
  687. * @return $this
  688. */
  689. public function disableHorizontal()
  690. {
  691. $this->horizontal = false;
  692. return $this;
  693. }
  694. /**
  695. * @return array
  696. */
  697. public function viewElementClasses()
  698. {
  699. if ($this->horizontal) {
  700. return [
  701. 'label' => "col-sm-{$this->width['label']} {$this->labelClass()}",
  702. 'field' => "col-sm-{$this->width['field']}",
  703. 'form-group' => 'form-group ',
  704. ];
  705. }
  706. return ['label' => $this->labelClass(), 'field' => '', 'form-group' => ''];
  707. }
  708. /**
  709. * Set form element class.
  710. *
  711. * @param string|array $class
  712. *
  713. * @return $this|array
  714. */
  715. public function elementClass($class = null)
  716. {
  717. if ($class === null) {
  718. if (! $this->elementClass) {
  719. $name = $this->elementName ?: $this->formatName($this->column);
  720. $this->elementClass = (array) str_replace(['[', ']'], '_', $name);
  721. }
  722. return $this->elementClass;
  723. }
  724. $this->elementClass = array_merge($this->elementClass, (array) $class);
  725. return $this;
  726. }
  727. /**
  728. * Get element class string.
  729. *
  730. * @return mixed
  731. */
  732. protected function elementClassString()
  733. {
  734. $elementClass = $this->elementClass();
  735. if (Arr::isAssoc($elementClass)) {
  736. $classes = [];
  737. foreach ($elementClass as $index => $class) {
  738. $classes[$index] = is_array($class) ? implode(' ', $class) : $class;
  739. }
  740. return $classes;
  741. }
  742. return implode(' ', $elementClass);
  743. }
  744. /**
  745. * Get element class selector.
  746. *
  747. * @return string|array
  748. */
  749. protected function elementClassSelector()
  750. {
  751. $elementClass = $this->elementClass();
  752. $formId = $this->formElementId();
  753. $formId = $formId ? '#'.$formId : '';
  754. if (Arr::isAssoc($elementClass)) {
  755. $classes = [];
  756. foreach ($elementClass as $index => $class) {
  757. $classes[$index] = $formId.' .'.(is_array($class) ? implode('.', $class) : $class);
  758. }
  759. return $classes;
  760. }
  761. return $formId.' .'.implode('.', $elementClass);
  762. }
  763. /**
  764. * Remove the field in modal.
  765. *
  766. * @return $this
  767. */
  768. public function hideInModal()
  769. {
  770. if (
  771. $this->form instanceof Form
  772. && $this->form->inModal()
  773. ) {
  774. $this->display(false);
  775. }
  776. return $this;
  777. }
  778. /**
  779. * @return string|null
  780. */
  781. protected function formElementId()
  782. {
  783. return $this->form ? $this->form->elementId() : null;
  784. }
  785. /**
  786. * Add the element class.
  787. *
  788. * @param $class
  789. *
  790. * @return $this
  791. */
  792. public function addElementClass($class)
  793. {
  794. $this->elementClass = array_unique(
  795. array_merge($this->elementClass, (array) $class)
  796. );
  797. return $this;
  798. }
  799. /**
  800. * Remove element class.
  801. *
  802. * @param $class
  803. *
  804. * @return $this
  805. */
  806. public function removeElementClass($class)
  807. {
  808. $delClass = [];
  809. if (is_string($class) || is_array($class)) {
  810. $delClass = (array) $class;
  811. }
  812. foreach ($delClass as $del) {
  813. if (($key = array_search($del, $this->elementClass))) {
  814. unset($this->elementClass[$key]);
  815. }
  816. }
  817. return $this;
  818. }
  819. /**
  820. * Add variables to field view.
  821. *
  822. * @param array $variables
  823. *
  824. * @return $this
  825. */
  826. protected function addVariables(array $variables = [])
  827. {
  828. $this->variables = array_merge($this->variables, $variables);
  829. return $this;
  830. }
  831. /**
  832. * @param array|string $labelClass
  833. * @param bool $append
  834. *
  835. * @return $this|string
  836. */
  837. public function labelClass($labelClass = null, bool $append = true)
  838. {
  839. if ($labelClass === null) {
  840. return implode(' ', $this->labelClass);
  841. }
  842. $this->labelClass = $append
  843. ? array_unique(array_merge($this->labelClass, (array) $labelClass))
  844. : (array) $labelClass;
  845. return $this;
  846. }
  847. /**
  848. * Get the view variables of this field.
  849. *
  850. * @return array
  851. */
  852. public function variables()
  853. {
  854. return array_merge($this->variables, [
  855. 'id' => $this->id,
  856. 'name' => $this->elementName(),
  857. 'help' => $this->help,
  858. 'class' => $this->elementClassString(),
  859. 'value' => $this->value(),
  860. 'label' => $this->label,
  861. 'viewClass' => $this->viewElementClasses(),
  862. 'column' => $this->column,
  863. 'errorKey' => $this->errorKey(),
  864. 'attributes' => $this->formatAttributes(),
  865. 'placeholder' => $this->placeholder(),
  866. 'disabled' => $this->attributes['disabled'] ?? false,
  867. 'formId' => $this->formElementId(),
  868. ]);
  869. }
  870. /**
  871. * Get view of this field.
  872. *
  873. * @return string
  874. */
  875. public function getView()
  876. {
  877. return $this->view ?: 'admin::form.'.strtolower(class_basename(static::class));
  878. }
  879. /**
  880. * Set view of current field.
  881. *
  882. * @return string
  883. */
  884. public function view($view)
  885. {
  886. $this->view = $view;
  887. return $this;
  888. }
  889. /**
  890. * Get script of current field.
  891. *
  892. * @return string
  893. */
  894. public function getScript()
  895. {
  896. return $this->script;
  897. }
  898. /**
  899. * Set script of current field.
  900. *
  901. * @return self
  902. */
  903. public function script($script)
  904. {
  905. $this->script = $script;
  906. return $this;
  907. }
  908. /**
  909. * To set this field should render or not.
  910. *
  911. * @return self
  912. */
  913. public function display(bool $display)
  914. {
  915. $this->display = $display;
  916. return $this;
  917. }
  918. /**
  919. * If this field should render.
  920. *
  921. * @return bool
  922. */
  923. protected function shouldRender()
  924. {
  925. return $this->display;
  926. }
  927. /**
  928. * Collect assets required by this field.
  929. */
  930. public static function collectAssets()
  931. {
  932. static::$js && Admin::js(static::$js);
  933. static::$css && Admin::css(static::$css);
  934. }
  935. /**
  936. * Render this filed.
  937. *
  938. * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View|string
  939. */
  940. public function render()
  941. {
  942. if (! $this->shouldRender()) {
  943. return '';
  944. }
  945. Admin::script($this->script);
  946. return view($this->getView(), $this->variables());
  947. }
  948. /**
  949. * @return string
  950. */
  951. public function __toString()
  952. {
  953. $view = $this->render();
  954. return $view instanceof Renderable ? $view->render() : (string) $view;
  955. }
  956. }