Field.php 22 KB

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