Field.php 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131
  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. if (! $this->data || is_array($this->data)) {
  456. $this->data = new Fluent((array) $this->data);
  457. }
  458. return $this->data;
  459. }
  460. $this->data = new Fluent($data);
  461. return $this;
  462. }
  463. /**
  464. * Get or set default value for field.
  465. *
  466. * @param $default
  467. *
  468. * @return $this
  469. */
  470. public function default($default = null)
  471. {
  472. if ($default === null) {
  473. if ($this->default instanceof \Closure) {
  474. return call_user_func($this->default, $this->form);
  475. }
  476. return $this->default;
  477. }
  478. $this->default = $default;
  479. return $this;
  480. }
  481. /**
  482. * Set help block for current field.
  483. *
  484. * @param string $text
  485. * @param string $icon
  486. *
  487. * @return $this
  488. */
  489. public function help($text = '', $icon = 'fa-info-circle')
  490. {
  491. $this->help = compact('text', 'icon');
  492. return $this;
  493. }
  494. /**
  495. * Get column of the field.
  496. *
  497. * @return string|array
  498. */
  499. public function column()
  500. {
  501. return $this->column;
  502. }
  503. /**
  504. * Get or set label of the field.
  505. *
  506. * @param null $label
  507. *
  508. * @return $this|string
  509. */
  510. public function label($label = null)
  511. {
  512. if ($label == null) {
  513. return $this->label;
  514. }
  515. if ($label instanceof \Closure) {
  516. $label = $label($this->label);
  517. }
  518. $this->label = $label;
  519. return $this;
  520. }
  521. public function old()
  522. {
  523. return old($this->column, $this->value());
  524. }
  525. /**
  526. * Get original value of the field.
  527. *
  528. * @return mixed
  529. */
  530. public function original()
  531. {
  532. return $this->original;
  533. }
  534. /**
  535. * Sanitize input data.
  536. *
  537. * @param array $input
  538. * @param string $column
  539. *
  540. * @return array
  541. */
  542. protected function sanitizeInput($input, $column)
  543. {
  544. if ($this instanceof Field\MultipleSelect) {
  545. $value = Arr::get($input, $column);
  546. Arr::set($input, $column, array_filter($value));
  547. }
  548. return $input;
  549. }
  550. /**
  551. * Add html attributes to elements.
  552. *
  553. * @param array|string $attribute
  554. * @param mixed $value
  555. *
  556. * @return $this
  557. */
  558. public function attribute($attribute, $value = null)
  559. {
  560. if (is_array($attribute)) {
  561. $this->attributes = array_merge($this->attributes, $attribute);
  562. } else {
  563. $this->attributes[$attribute] = (string) $value;
  564. }
  565. return $this;
  566. }
  567. /**
  568. * Specifies a regular expression against which to validate the value of the input.
  569. *
  570. * @param string $error
  571. * @param string $regexp
  572. *
  573. * @return $this
  574. */
  575. public function pattern($regexp, $error = null)
  576. {
  577. if ($error) {
  578. $this->attribute('data-pattern-error', $error);
  579. }
  580. return $this->attribute('pattern', $regexp);
  581. }
  582. /**
  583. * set the input filed required.
  584. *
  585. * @param bool $isLabelAsterisked
  586. *
  587. * @return $this
  588. */
  589. public function required($isLabelAsterisked = true)
  590. {
  591. if ($isLabelAsterisked) {
  592. $this->labelClass(['asterisk']);
  593. }
  594. $this->rules('required');
  595. return $this->attribute('required', true);
  596. }
  597. /**
  598. * Set the field automatically get focus.
  599. *
  600. * @return $this
  601. */
  602. public function autofocus()
  603. {
  604. return $this->attribute('autofocus', true);
  605. }
  606. /**
  607. * Set the field as readonly mode.
  608. *
  609. * @return $this
  610. */
  611. public function readOnly()
  612. {
  613. return $this->attribute('readonly', true);
  614. }
  615. /**
  616. * Set field as disabled.
  617. *
  618. * @return $this
  619. */
  620. public function disable()
  621. {
  622. return $this->attribute('disabled', true);
  623. }
  624. /**
  625. * Get or set field placeholder.
  626. *
  627. * @param string $placeholder
  628. *
  629. * @return $this|string
  630. */
  631. public function placeholder($placeholder = null)
  632. {
  633. if ($placeholder === null) {
  634. return $this->placeholder ?: trans('admin.input').' '.$this->label;
  635. }
  636. $this->placeholder = $placeholder;
  637. return $this;
  638. }
  639. /**
  640. * Prepare for a field value before update or insert.
  641. *
  642. * @param mixed $value
  643. *
  644. * @return mixed
  645. */
  646. protected function prepareToSave($value)
  647. {
  648. return $value;
  649. }
  650. /**
  651. * @param \Closure $closure
  652. *
  653. * @return $this
  654. */
  655. public function saving(\Closure $closure)
  656. {
  657. $this->prepareCallback = $closure;
  658. return $this;
  659. }
  660. /**
  661. * Prepare for a field value before update or insert.
  662. *
  663. * @param mixed $value
  664. *
  665. * @return mixed
  666. */
  667. final public function prepare($value)
  668. {
  669. $value = $this->prepareToSave($value);
  670. if ($handler = $this->prepareCallback) {
  671. $handler->bindTo($this->data());
  672. return $handler($value);
  673. }
  674. return $value;
  675. }
  676. /**
  677. * Format the field attributes.
  678. *
  679. * @return string
  680. */
  681. protected function formatAttributes()
  682. {
  683. $html = [];
  684. foreach ($this->attributes as $name => $value) {
  685. $html[] = $name.'="'.e($value).'"';
  686. }
  687. return implode(' ', $html);
  688. }
  689. /**
  690. * @return $this
  691. */
  692. public function disableHorizontal()
  693. {
  694. $this->horizontal = false;
  695. return $this;
  696. }
  697. /**
  698. * @return array
  699. */
  700. public function viewElementClasses()
  701. {
  702. if ($this->horizontal) {
  703. return [
  704. 'label' => "col-sm-{$this->width['label']} {$this->labelClass()}",
  705. 'field' => "col-sm-{$this->width['field']}",
  706. 'form-group' => 'form-group ',
  707. ];
  708. }
  709. return ['label' => $this->labelClass(), 'field' => '', 'form-group' => ''];
  710. }
  711. /**
  712. * Set form element class.
  713. *
  714. * @param string|array $class
  715. *
  716. * @return $this|array
  717. */
  718. public function elementClass($class = null)
  719. {
  720. if ($class === null) {
  721. if (! $this->elementClass) {
  722. $name = $this->elementName ?: $this->formatName($this->column);
  723. $this->elementClass = (array) str_replace(['[', ']'], '_', $name);
  724. }
  725. return $this->elementClass;
  726. }
  727. $this->elementClass = array_merge($this->elementClass, (array) $class);
  728. return $this;
  729. }
  730. /**
  731. * Get element class string.
  732. *
  733. * @return mixed
  734. */
  735. protected function elementClassString()
  736. {
  737. $elementClass = $this->elementClass();
  738. if (Arr::isAssoc($elementClass)) {
  739. $classes = [];
  740. foreach ($elementClass as $index => $class) {
  741. $classes[$index] = is_array($class) ? implode(' ', $class) : $class;
  742. }
  743. return $classes;
  744. }
  745. return implode(' ', $elementClass);
  746. }
  747. /**
  748. * Get element class selector.
  749. *
  750. * @return string|array
  751. */
  752. protected function elementClassSelector()
  753. {
  754. $elementClass = $this->elementClass();
  755. $formId = $this->formElementId();
  756. $formId = $formId ? '#'.$formId : '';
  757. if (Arr::isAssoc($elementClass)) {
  758. $classes = [];
  759. foreach ($elementClass as $index => $class) {
  760. $classes[$index] = $formId.' .'.(is_array($class) ? implode('.', $class) : $class);
  761. }
  762. return $classes;
  763. }
  764. return $formId.' .'.implode('.', $elementClass);
  765. }
  766. /**
  767. * Remove the field in modal.
  768. *
  769. * @return $this
  770. */
  771. public function hideInModal()
  772. {
  773. if (
  774. $this->form instanceof Form
  775. && $this->form->inModal()
  776. ) {
  777. $this->display(false);
  778. }
  779. return $this;
  780. }
  781. /**
  782. * @return string|null
  783. */
  784. protected function formElementId()
  785. {
  786. return $this->form ? $this->form->elementId() : null;
  787. }
  788. /**
  789. * Add the element class.
  790. *
  791. * @param $class
  792. *
  793. * @return $this
  794. */
  795. public function addElementClass($class)
  796. {
  797. $this->elementClass = array_unique(
  798. array_merge($this->elementClass, (array) $class)
  799. );
  800. return $this;
  801. }
  802. /**
  803. * Remove element class.
  804. *
  805. * @param $class
  806. *
  807. * @return $this
  808. */
  809. public function removeElementClass($class)
  810. {
  811. $delClass = [];
  812. if (is_string($class) || is_array($class)) {
  813. $delClass = (array) $class;
  814. }
  815. foreach ($delClass as $del) {
  816. if (($key = array_search($del, $this->elementClass))) {
  817. unset($this->elementClass[$key]);
  818. }
  819. }
  820. return $this;
  821. }
  822. /**
  823. * Add variables to field view.
  824. *
  825. * @param array $variables
  826. *
  827. * @return $this
  828. */
  829. protected function addVariables(array $variables = [])
  830. {
  831. $this->variables = array_merge($this->variables, $variables);
  832. return $this;
  833. }
  834. /**
  835. * @param array|string $labelClass
  836. * @param bool $append
  837. *
  838. * @return $this|string
  839. */
  840. public function labelClass($labelClass = null, bool $append = true)
  841. {
  842. if ($labelClass === null) {
  843. return implode(' ', $this->labelClass);
  844. }
  845. $this->labelClass = $append
  846. ? array_unique(array_merge($this->labelClass, (array) $labelClass))
  847. : (array) $labelClass;
  848. return $this;
  849. }
  850. /**
  851. * Get the view variables of this field.
  852. *
  853. * @return array
  854. */
  855. public function variables()
  856. {
  857. return array_merge($this->variables, [
  858. 'id' => $this->id,
  859. 'name' => $this->elementName(),
  860. 'help' => $this->help,
  861. 'class' => $this->elementClassString(),
  862. 'value' => $this->value(),
  863. 'label' => $this->label,
  864. 'viewClass' => $this->viewElementClasses(),
  865. 'column' => $this->column,
  866. 'errorKey' => $this->errorKey(),
  867. 'attributes' => $this->formatAttributes(),
  868. 'placeholder' => $this->placeholder(),
  869. 'disabled' => $this->attributes['disabled'] ?? false,
  870. 'formId' => $this->formElementId(),
  871. ]);
  872. }
  873. /**
  874. * Get view of this field.
  875. *
  876. * @return string
  877. */
  878. public function getView()
  879. {
  880. return $this->view ?: 'admin::form.'.strtolower(class_basename(static::class));
  881. }
  882. /**
  883. * Set view of current field.
  884. *
  885. * @return string
  886. */
  887. public function view($view)
  888. {
  889. $this->view = $view;
  890. return $this;
  891. }
  892. /**
  893. * Get script of current field.
  894. *
  895. * @return string
  896. */
  897. public function getScript()
  898. {
  899. return $this->script;
  900. }
  901. /**
  902. * Set script of current field.
  903. *
  904. * @return self
  905. */
  906. public function script($script)
  907. {
  908. $this->script = $script;
  909. return $this;
  910. }
  911. /**
  912. * To set this field should render or not.
  913. *
  914. * @return self
  915. */
  916. public function display(bool $display)
  917. {
  918. $this->display = $display;
  919. return $this;
  920. }
  921. /**
  922. * If this field should render.
  923. *
  924. * @return bool
  925. */
  926. protected function shouldRender()
  927. {
  928. return $this->display;
  929. }
  930. /**
  931. * Collect assets required by this field.
  932. */
  933. public static function collectAssets()
  934. {
  935. static::$js && Admin::js(static::$js);
  936. static::$css && Admin::css(static::$css);
  937. }
  938. /**
  939. * Render this filed.
  940. *
  941. * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View|string
  942. */
  943. public function render()
  944. {
  945. if (! $this->shouldRender()) {
  946. return '';
  947. }
  948. Admin::script($this->script);
  949. return view($this->getView(), $this->variables());
  950. }
  951. /**
  952. * @return string
  953. */
  954. public function __toString()
  955. {
  956. $view = $this->render();
  957. return $view instanceof Renderable ? $view->render() : (string) $view;
  958. }
  959. }