Field.php 21 KB

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