Field.php 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134
  1. <?php
  2. namespace Dcat\Admin\Form;
  3. use Dcat\Admin\Admin;
  4. use Dcat\Admin\Form;
  5. use Dcat\Admin\Form\Concerns;
  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\Traits\Macroable;
  11. /**
  12. * Class Field.
  13. */
  14. class Field implements Renderable
  15. {
  16. use Macroable,
  17. 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
  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 $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
  194. */
  195. public function getElementId()
  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. if (is_array($column)) {
  209. return str_replace('.', '-', $column);
  210. }
  211. return 'form-field-'.str_replace('.', '-', $column);
  212. }
  213. /**
  214. * Format the label value.
  215. *
  216. * @param array $arguments
  217. *
  218. * @return string
  219. */
  220. protected function formatLabel($arguments = [])
  221. {
  222. $column = is_array($this->column) ? current($this->column) : $this->column;
  223. $label = isset($arguments[0]) ? $arguments[0] : ucfirst(admin_trans_field($column));
  224. return str_replace(['.', '_'], ' ', $label);
  225. }
  226. /**
  227. * Format the name of the field.
  228. *
  229. * @param string $column
  230. *
  231. * @return array|mixed|string
  232. */
  233. protected function formatName($column)
  234. {
  235. if (is_string($column)) {
  236. $name = explode('.', $column);
  237. if (count($name) == 1) {
  238. return $name[0];
  239. }
  240. $html = array_shift($name);
  241. foreach ($name as $piece) {
  242. $html .= "[$piece]";
  243. }
  244. return $html;
  245. }
  246. if (is_array($this->column)) {
  247. $names = [];
  248. foreach ($this->column as $key => $name) {
  249. $names[$key] = $this->formatName($name);
  250. }
  251. return $names;
  252. }
  253. return '';
  254. }
  255. /**
  256. * Set form element name.
  257. *
  258. * @param string $name
  259. *
  260. * @return $this
  261. *
  262. * @author Edwin Hui
  263. */
  264. public function setElementName($name)
  265. {
  266. $this->elementName = $name;
  267. return $this;
  268. }
  269. /**
  270. * Fill data to the field.
  271. *
  272. * @param array $data
  273. *
  274. * @return void
  275. */
  276. final public function fill($data)
  277. {
  278. $this->data($data);
  279. $this->value = $this->formatFieldData($data);
  280. $this->callCustomFormatter();
  281. }
  282. /**
  283. * Format field data.
  284. *
  285. * @param array $data
  286. * @return mixed
  287. */
  288. protected function formatFieldData($data)
  289. {
  290. if (is_array($this->column)) {
  291. $value = [];
  292. foreach ($this->column as $key => $column) {
  293. $value[$key] = Arr::get($data, $column);
  294. }
  295. return $value;
  296. }
  297. return Arr::get($data, $this->column, $this->value);
  298. }
  299. /**
  300. * custom format form column data when edit.
  301. *
  302. * @param \Closure $call
  303. *
  304. * @return $this
  305. */
  306. public function customFormat(\Closure $call)
  307. {
  308. $this->customFormat = $call;
  309. return $this;
  310. }
  311. /**
  312. * Set original value to the field.
  313. *
  314. * @param array $data
  315. * @return void
  316. */
  317. final public function setOriginal($data)
  318. {
  319. $this->original = $this->formatFieldData($data);
  320. $this->callCustomFormatter('original', new Fluent($data));
  321. }
  322. /**
  323. * @param string $key
  324. * @param Fluent|null $dataremoveField
  325. */
  326. protected function callCustomFormatter($key = 'value', Fluent $data = null)
  327. {
  328. if ($this->customFormat) {
  329. $this->{$key} = $this->customFormat
  330. ->call(
  331. $data ?: $this->data(),
  332. $this->{$key},
  333. $this->column,
  334. $this
  335. );
  336. }
  337. }
  338. /**
  339. * @param Form $form
  340. *
  341. * @return $this
  342. */
  343. public function setForm(Form $form = null)
  344. {
  345. $this->form = $form;
  346. return $this;
  347. }
  348. /**
  349. * @return Fluent
  350. */
  351. public function getFormModel()
  352. {
  353. return $this->form ? $this->form->model() : new Fluent;
  354. }
  355. /**
  356. * Set width for field and label.
  357. *
  358. * @param int $field
  359. * @param int $label
  360. *
  361. * @return $this
  362. */
  363. public function setWidth($field = 8, $label = 2)
  364. {
  365. $this->width = [
  366. 'label' => $label,
  367. 'field' => $field,
  368. ];
  369. return $this;
  370. }
  371. /**
  372. * Set the field options.
  373. *
  374. * @param array $options
  375. *
  376. * @return $this
  377. */
  378. public function options($options = [])
  379. {
  380. if ($options instanceof Arrayable) {
  381. $options = $options->toArray();
  382. }
  383. $this->options = array_merge($this->options, $options);
  384. return $this;
  385. }
  386. /**
  387. * Set the field option checked.
  388. *
  389. * @param array $checked
  390. *
  391. * @return $this
  392. */
  393. public function checked($checked = [])
  394. {
  395. if ($checked instanceof Arrayable) {
  396. $checked = $checked->toArray();
  397. }
  398. $this->checked = array_merge($this->checked, (array)$checked);
  399. return $this;
  400. }
  401. /**
  402. * Get key for error message.
  403. *
  404. * @return string
  405. */
  406. public function getErrorKey()
  407. {
  408. return $this->errorKey ?: $this->column;
  409. }
  410. /**
  411. * Set key for error message.
  412. *
  413. * @param string $key
  414. *
  415. * @return $this
  416. */
  417. public function setErrorKey($key)
  418. {
  419. $this->errorKey = $key;
  420. return $this;
  421. }
  422. /**
  423. * Set or get value of the field.
  424. *
  425. * @param null $value
  426. *
  427. * @return mixed
  428. */
  429. public function value($value = null)
  430. {
  431. if (is_null($value)) {
  432. return is_null($this->value) ? $this->getDefault() : $this->value;
  433. }
  434. $this->value = $value;
  435. return $this;
  436. }
  437. /**
  438. * Set or get data.
  439. *
  440. * @param array $data
  441. *
  442. * @return $this
  443. */
  444. public function data(array $data = null)
  445. {
  446. if (is_null($data)) {
  447. return $this->data ?: ($this->data = new Fluent);
  448. }
  449. $this->data = new Fluent($data);
  450. return $this;
  451. }
  452. /**
  453. * Set default value for field.
  454. *
  455. * @param $default
  456. *
  457. * @return $this
  458. */
  459. public function default($default)
  460. {
  461. $this->default = $default;
  462. return $this;
  463. }
  464. /**
  465. * Get default value.
  466. *
  467. * @return mixed
  468. */
  469. public function getDefault()
  470. {
  471. if ($this->default instanceof \Closure) {
  472. return call_user_func($this->default, $this->form);
  473. }
  474. return $this->default;
  475. }
  476. /**
  477. * Set help block for current field.
  478. *
  479. * @param string $text
  480. * @param string $icon
  481. *
  482. * @return $this
  483. */
  484. public function help($text = '', $icon = 'fa-info-circle')
  485. {
  486. $this->help = compact('text', 'icon');
  487. return $this;
  488. }
  489. /**
  490. * Get column of the field.
  491. *
  492. * @return string|array
  493. */
  494. public function column()
  495. {
  496. return $this->column;
  497. }
  498. /**
  499. * Get or set label of the field.
  500. *
  501. * @param null $label
  502. * @return $this|string
  503. */
  504. public function label($label = null)
  505. {
  506. if ($label == null) {
  507. return $this->label;
  508. }
  509. if ($label instanceof \Closure) {
  510. $label = $label($this->label);
  511. }
  512. $this->label = $label;
  513. return $this;
  514. }
  515. public function old()
  516. {
  517. return old($this->column, $this->value());
  518. }
  519. /**
  520. * Get original value of the field.
  521. *
  522. * @return mixed
  523. */
  524. public function original()
  525. {
  526. return $this->original;
  527. }
  528. /**
  529. * Sanitize input data.
  530. *
  531. * @param array $input
  532. * @param string $column
  533. *
  534. * @return array
  535. */
  536. protected function sanitizeInput($input, $column)
  537. {
  538. if ($this instanceof Field\MultipleSelect) {
  539. $value = Arr::get($input, $column);
  540. Arr::set($input, $column, array_filter($value));
  541. }
  542. return $input;
  543. }
  544. /**
  545. * Add html attributes to elements.
  546. *
  547. * @param array|string $attribute
  548. * @param mixed $value
  549. *
  550. * @return $this
  551. */
  552. public function attribute($attribute, $value = null)
  553. {
  554. if (is_array($attribute)) {
  555. $this->attributes = array_merge($this->attributes, $attribute);
  556. } else {
  557. $this->attributes[$attribute] = (string) $value;
  558. }
  559. return $this;
  560. }
  561. /**
  562. * Specifies a regular expression against which to validate the value of the input.
  563. *
  564. * @param string $regexp
  565. *
  566. * @return $this
  567. */
  568. public function pattern($regexp)
  569. {
  570. return $this->attribute('pattern', $regexp);
  571. }
  572. /**
  573. * set the input filed required.
  574. *
  575. * @param bool $isLabelAsterisked
  576. *
  577. * @return $this
  578. */
  579. public function required($isLabelAsterisked = true)
  580. {
  581. if ($isLabelAsterisked) {
  582. $this->setLabelClass(['asterisk']);
  583. }
  584. $this->rules('required');
  585. return $this->attribute('required', true);
  586. }
  587. /**
  588. * Set the field automatically get focus.
  589. *
  590. * @return $this
  591. */
  592. public function autofocus()
  593. {
  594. return $this->attribute('autofocus', true);
  595. }
  596. /**
  597. * Set the field as readonly mode.
  598. *
  599. * @return $this
  600. */
  601. public function readOnly()
  602. {
  603. return $this->attribute('readonly', true);
  604. }
  605. /**
  606. * Set field as disabled.
  607. *
  608. * @return $this
  609. */
  610. public function disable()
  611. {
  612. return $this->attribute('disabled', true);
  613. }
  614. /**
  615. * Set field placeholder.
  616. *
  617. * @param string $placeholder
  618. *
  619. * @return $this
  620. */
  621. public function placeholder($placeholder = '')
  622. {
  623. $this->placeholder = $placeholder;
  624. return $this;
  625. }
  626. /**
  627. * Get placeholder.
  628. *
  629. * @return string
  630. */
  631. public function getPlaceholder()
  632. {
  633. return $this->placeholder ?: trans('admin.input').' '.$this->label;
  634. }
  635. /**
  636. * Prepare for a field value before update or insert.
  637. *
  638. * @param mixed $value
  639. * @return mixed
  640. */
  641. public function prepareToSave($value)
  642. {
  643. return $value;
  644. }
  645. /**
  646. * @param \Closure $closure
  647. * @return $this
  648. */
  649. public function saving(\Closure $closure)
  650. {
  651. $this->prepareCallback = $closure;
  652. return $this;
  653. }
  654. /**
  655. * Prepare for a field value before update or insert.
  656. *
  657. * @param mixed $value
  658. * @return mixed
  659. */
  660. final public function prepareInputValue($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 getViewElementClasses()
  694. {
  695. if ($this->horizontal) {
  696. return [
  697. 'label' => "col-sm-{$this->width['label']} {$this->getLabelClass()}",
  698. 'field' => "col-sm-{$this->width['field']}",
  699. 'form-group' => 'form-group ',
  700. ];
  701. }
  702. return ['label' => $this->getLabelClass(), 'field' => '', 'form-group' => ''];
  703. }
  704. /**
  705. * Set form element class.
  706. *
  707. * @param string|array $class
  708. *
  709. * @return $this
  710. */
  711. public function setElementClass($class)
  712. {
  713. $this->elementClass = array_merge($this->elementClass, (array) $class);
  714. return $this;
  715. }
  716. /**
  717. * Get element class.
  718. *
  719. * @return array
  720. */
  721. protected function getElementClass()
  722. {
  723. if (!$this->elementClass) {
  724. $name = $this->elementName ?: $this->formatName($this->column);
  725. $this->elementClass = (array) str_replace(['[', ']'], '_', $name);
  726. }
  727. return $this->elementClass;
  728. }
  729. /**
  730. * Get element class string.
  731. *
  732. * @return mixed
  733. */
  734. protected function getElementClassString()
  735. {
  736. $elementClass = $this->getElementClass();
  737. if (Arr::isAssoc($elementClass)) {
  738. $classes = [];
  739. foreach ($elementClass as $index => $class) {
  740. $classes[$index] = is_array($class) ? implode(' ', $class) : $class;
  741. }
  742. return $classes;
  743. }
  744. return implode(' ', $elementClass);
  745. }
  746. /**
  747. * Get element class selector.
  748. *
  749. * @return string|array
  750. */
  751. protected function getElementClassSelector()
  752. {
  753. $elementClass = $this->getElementClass();
  754. $formId = $this->getFormId();
  755. $formId = $formId ? '#'.$formId : '';
  756. if (Arr::isAssoc($elementClass)) {
  757. $classes = [];
  758. foreach ($elementClass as $index => $class) {
  759. $classes[$index] = $formId . ' .'.(is_array($class) ? implode('.', $class) : $class);
  760. }
  761. return $classes;
  762. }
  763. return $formId . ' .'.implode('.', $elementClass);
  764. }
  765. /**
  766. * Remove the field in modal.
  767. *
  768. * @return $this
  769. */
  770. public function hideInModal()
  771. {
  772. if (
  773. $this->form instanceof Form
  774. && $this->form->inModal()
  775. ) {
  776. $this->setDisplay(false);
  777. }
  778. return $this;
  779. }
  780. /**
  781. * @return string|null
  782. */
  783. protected function getFormId()
  784. {
  785. return $this->form ? $this->form->getFormId() : null;
  786. }
  787. /**
  788. * Add the element class.
  789. *
  790. * @param $class
  791. *
  792. * @return $this
  793. */
  794. public function addElementClass($class)
  795. {
  796. if (is_array($class) || is_string($class)) {
  797. $this->elementClass = array_merge($this->elementClass, (array) $class);
  798. $this->elementClass = array_unique($this->elementClass);
  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. * @return string
  836. */
  837. public function getLabelClass(): string
  838. {
  839. return implode(' ', $this->labelClass);
  840. }
  841. /**
  842. * @param array $labelClass
  843. *
  844. * @return $this
  845. */
  846. public function setLabelClass(array $labelClass)
  847. {
  848. $this->labelClass = $labelClass;
  849. return $this;
  850. }
  851. /**
  852. * @return string
  853. */
  854. public function getElementName()
  855. {
  856. return $this->elementName ?: $this->formatName($this->column);
  857. }
  858. /**
  859. * Get the view variables of this field.
  860. *
  861. * @return array
  862. */
  863. public function variables()
  864. {
  865. return array_merge($this->variables, [
  866. 'id' => $this->id,
  867. 'name' => $this->getElementName(),
  868. 'help' => $this->help,
  869. 'class' => $this->getElementClassString(),
  870. 'value' => $this->value(),
  871. 'label' => $this->label,
  872. 'viewClass' => $this->getViewElementClasses(),
  873. 'column' => $this->column,
  874. 'errorKey' => $this->getErrorKey(),
  875. 'attributes' => $this->formatAttributes(),
  876. 'placeholder' => $this->getPlaceholder(),
  877. 'disabled' => $this->attributes['disabled'] ?? false,
  878. 'formId' => $this->getFormId(),
  879. ]);
  880. }
  881. /**
  882. * Get view of this field.
  883. *
  884. * @return string
  885. */
  886. public function getView()
  887. {
  888. return $this->view ?: 'admin::form.'.strtolower(class_basename(static::class));
  889. }
  890. /**
  891. * Set view of current field.
  892. *
  893. * @return string
  894. */
  895. public function setView($view)
  896. {
  897. $this->view = $view;
  898. return $this;
  899. }
  900. /**
  901. * Get script of current field.
  902. *
  903. * @return string
  904. */
  905. public function getScript()
  906. {
  907. return $this->script;
  908. }
  909. /**
  910. * Set script of current field.
  911. *
  912. * @return self
  913. */
  914. public function setScript($script)
  915. {
  916. $this->script = $script;
  917. return $this;
  918. }
  919. /**
  920. * To set this field should render or not.
  921. *
  922. * @return self
  923. */
  924. public function setDisplay(bool $display)
  925. {
  926. $this->display = $display;
  927. return $this;
  928. }
  929. /**
  930. * If this field should render.
  931. *
  932. * @return bool
  933. */
  934. protected function shouldRender()
  935. {
  936. return $this->display;
  937. }
  938. /**
  939. * Collect assets required by this field.
  940. */
  941. public static function collectAssets()
  942. {
  943. static::$js && Admin::js(static::$js);
  944. static::$css && Admin::css(static::$css);
  945. }
  946. /**
  947. * Render this filed.
  948. *
  949. * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View|string
  950. */
  951. public function render()
  952. {
  953. if (!$this->shouldRender()) {
  954. return '';
  955. }
  956. Admin::script($this->script);
  957. return view($this->getView(), $this->variables());
  958. }
  959. /**
  960. * @return string
  961. */
  962. public function __toString()
  963. {
  964. return $this->render()->render();
  965. }
  966. }