Field.php 23 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177
  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 $savingCallback;
  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 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. $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] : 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 setElementName(string $name)
  270. {
  271. $this->elementName = $name;
  272. return $this;
  273. }
  274. /**
  275. * Get form element name.
  276. *
  277. * @return array|mixed|string
  278. */
  279. public function getElementName()
  280. {
  281. return $this->elementName ?: $this->formatName($this->column);
  282. }
  283. /**
  284. * Fill data to the field.
  285. *
  286. * @param array $data
  287. *
  288. * @return void
  289. */
  290. final public function fill($data)
  291. {
  292. $this->data($data);
  293. $this->value = $this->formatFieldData($data);
  294. $this->callCustomFormatter();
  295. }
  296. /**
  297. * Format field data.
  298. *
  299. * @param array $data
  300. *
  301. * @return mixed
  302. */
  303. protected function formatFieldData($data)
  304. {
  305. if (is_array($this->column)) {
  306. $value = [];
  307. foreach ($this->column as $key => $column) {
  308. $value[$key] = Arr::get($data, $column);
  309. }
  310. return $value;
  311. }
  312. return Arr::get($data, $this->column, $this->value);
  313. }
  314. /**
  315. * custom format form column data when edit.
  316. *
  317. * @param \Closure $call
  318. *
  319. * @return $this
  320. */
  321. public function customFormat(\Closure $call)
  322. {
  323. $this->customFormat = $call;
  324. return $this;
  325. }
  326. /**
  327. * Set original value to the field.
  328. *
  329. * @param array $data
  330. *
  331. * @return void
  332. */
  333. final public function setOriginal($data)
  334. {
  335. $this->original = $this->formatFieldData($data);
  336. $this->callCustomFormatter('original', new Fluent($data));
  337. }
  338. /**
  339. * @param string $key
  340. * @param Fluent|null $dataremoveField
  341. */
  342. protected function callCustomFormatter($key = 'value', Fluent $data = null)
  343. {
  344. if ($this->customFormat) {
  345. $this->{$key} = $this->customFormat
  346. ->call(
  347. $data ?: $this->data(),
  348. $this->{$key},
  349. $this->column,
  350. $this
  351. );
  352. }
  353. }
  354. /**
  355. * @param Form|WidgetForm $form
  356. *
  357. * @return $this
  358. */
  359. public function setForm($form = null)
  360. {
  361. $this->form = $form;
  362. return $this;
  363. }
  364. /**
  365. * @return Fluent
  366. */
  367. public function values()
  368. {
  369. return $this->form ? $this->form->model() : new Fluent();
  370. }
  371. /**
  372. * Set width for field and label.
  373. *
  374. * @param int $field
  375. * @param int $label
  376. *
  377. * @return $this
  378. */
  379. public function width($field = 8, $label = 2)
  380. {
  381. $this->width = [
  382. 'label' => $label,
  383. 'field' => $field,
  384. ];
  385. return $this;
  386. }
  387. /**
  388. * Set the field options.
  389. *
  390. * @param array $options
  391. *
  392. * @return $this
  393. */
  394. public function options($options = [])
  395. {
  396. if ($options instanceof Arrayable) {
  397. $options = $options->toArray();
  398. }
  399. if (is_array($this->options)) {
  400. $this->options = array_merge($this->options, $options);
  401. } else {
  402. $this->options = $options;
  403. }
  404. return $this;
  405. }
  406. /**
  407. * Set the field option checked.
  408. *
  409. * @param array $checked
  410. *
  411. * @return $this
  412. */
  413. public function checked($checked = [])
  414. {
  415. if ($checked instanceof Arrayable) {
  416. $checked = $checked->toArray();
  417. }
  418. $this->checked = array_merge($this->checked, (array) $checked);
  419. return $this;
  420. }
  421. /**
  422. * Set key for error message.
  423. *
  424. * @param string $key
  425. *
  426. * @return $this|string
  427. */
  428. public function setErrorKey(string $key)
  429. {
  430. $this->errorKey = $key;
  431. return $this;
  432. }
  433. /**
  434. * Get key for error message.
  435. *
  436. * @return string
  437. */
  438. public function getErrorKey()
  439. {
  440. return $this->errorKey ?: $this->column;
  441. }
  442. /**
  443. * Set or get value of the field.
  444. *
  445. * @param null $value
  446. *
  447. * @return mixed
  448. */
  449. public function value($value = null)
  450. {
  451. if (is_null($value)) {
  452. if (
  453. $this->value === null
  454. || (is_array($this->value) && empty($this->value))
  455. ) {
  456. return $this->default();
  457. }
  458. return $this->value;
  459. }
  460. $this->value = $value;
  461. return $this;
  462. }
  463. /**
  464. * Set or get data.
  465. *
  466. * @param array $data
  467. *
  468. * @return $this|Fluent
  469. */
  470. public function data(array $data = null)
  471. {
  472. if (is_null($data)) {
  473. if (! $this->data || is_array($this->data)) {
  474. $this->data = new Fluent((array) $this->data);
  475. }
  476. return $this->data;
  477. }
  478. $this->data = new Fluent($data);
  479. return $this;
  480. }
  481. /**
  482. * Get or set default value for field.
  483. *
  484. * @param $default
  485. *
  486. * @return $this|mixed
  487. */
  488. public function default($default = null)
  489. {
  490. if ($default === null) {
  491. if (
  492. $this->form
  493. && method_exists($this->form, 'isCreating')
  494. && ! $this->form->isCreating()
  495. ) {
  496. return;
  497. }
  498. if ($this->default instanceof \Closure) {
  499. return call_user_func($this->default, $this->form);
  500. }
  501. return $this->default;
  502. }
  503. $this->default = $default;
  504. return $this;
  505. }
  506. /**
  507. * Set help block for current field.
  508. *
  509. * @param string $text
  510. * @param string $icon
  511. *
  512. * @return $this
  513. */
  514. public function help($text = '', $icon = 'feather icon-help-circle')
  515. {
  516. $this->help = compact('text', 'icon');
  517. return $this;
  518. }
  519. /**
  520. * Get column of the field.
  521. *
  522. * @return string|array
  523. */
  524. public function column()
  525. {
  526. return $this->column;
  527. }
  528. /**
  529. * Get or set label of the field.
  530. *
  531. * @param null $label
  532. *
  533. * @return $this|string
  534. */
  535. public function label($label = null)
  536. {
  537. if ($label == null) {
  538. return $this->label;
  539. }
  540. if ($label instanceof \Closure) {
  541. $label = $label($this->label);
  542. }
  543. $this->label = $label;
  544. return $this;
  545. }
  546. /**
  547. * @return mixed
  548. */
  549. public function old()
  550. {
  551. return old($this->column, $this->value());
  552. }
  553. /**
  554. * Get original value of the field.
  555. *
  556. * @return mixed
  557. */
  558. public function original()
  559. {
  560. return $this->original;
  561. }
  562. /**
  563. * Sanitize input data.
  564. *
  565. * @param array $input
  566. * @param string $column
  567. *
  568. * @return array
  569. */
  570. protected function sanitizeInput($input, $column)
  571. {
  572. if ($this instanceof Field\MultipleSelect) {
  573. $value = Arr::get($input, $column);
  574. Arr::set($input, $column, array_filter($value));
  575. }
  576. return $input;
  577. }
  578. /**
  579. * Add html attributes to elements.
  580. *
  581. * @param array|string $attribute
  582. * @param mixed $value
  583. *
  584. * @return $this
  585. */
  586. public function attribute($attribute, $value = null)
  587. {
  588. if (is_array($attribute)) {
  589. $this->attributes = array_merge($this->attributes, $attribute);
  590. } else {
  591. $this->attributes[$attribute] = (string) $value;
  592. }
  593. return $this;
  594. }
  595. /**
  596. * @param string $key
  597. *
  598. * @return bool
  599. */
  600. public function hasAttribute(string $key)
  601. {
  602. return array_key_exists($key, $this->attributes);
  603. }
  604. /**
  605. * Specifies a regular expression against which to validate the value of the input.
  606. *
  607. * @param string $error
  608. * @param string $regexp
  609. *
  610. * @return $this
  611. */
  612. public function pattern($regexp, $error = null)
  613. {
  614. if ($error) {
  615. $this->attribute('data-pattern-error', $error);
  616. }
  617. return $this->attribute('pattern', $regexp);
  618. }
  619. /**
  620. * set the input filed required.
  621. *
  622. * @param bool $isLabelAsterisked
  623. *
  624. * @return $this
  625. */
  626. public function required($isLabelAsterisked = true)
  627. {
  628. if ($isLabelAsterisked) {
  629. $this->setLabelClass(['asterisk']);
  630. }
  631. $this->rules('required');
  632. return $this->attribute('required', true);
  633. }
  634. /**
  635. * Set the field automatically get focus.
  636. *
  637. * @return $this
  638. */
  639. public function autofocus()
  640. {
  641. return $this->attribute('autofocus', true);
  642. }
  643. /**
  644. * Set the field as readonly mode.
  645. *
  646. * @return $this
  647. */
  648. public function readOnly()
  649. {
  650. return $this->attribute('readonly', true);
  651. }
  652. /**
  653. * Set field as disabled.
  654. *
  655. * @return $this
  656. */
  657. public function disable()
  658. {
  659. return $this->attribute('disabled', true);
  660. }
  661. /**
  662. * Get or set field placeholder.
  663. *
  664. * @param string $placeholder
  665. *
  666. * @return $this|string
  667. */
  668. public function placeholder($placeholder = null)
  669. {
  670. if ($placeholder === null) {
  671. return $this->placeholder ?: trans('admin.input').' '.$this->label;
  672. }
  673. $this->placeholder = $placeholder;
  674. return $this;
  675. }
  676. /**
  677. * @param mixed $value
  678. *
  679. * @return mixed
  680. */
  681. protected function prepareInputValue($value)
  682. {
  683. return $value;
  684. }
  685. /**
  686. * @param \Closure $closure
  687. *
  688. * @return $this
  689. */
  690. public function saving(\Closure $closure)
  691. {
  692. $this->savingCallback = $closure;
  693. return $this;
  694. }
  695. /**
  696. * Prepare for a field value before update or insert.
  697. *
  698. * @param mixed $value
  699. *
  700. * @return mixed
  701. */
  702. final public function prepare($value)
  703. {
  704. $value = $this->prepareInputValue($value);
  705. if ($handler = $this->savingCallback) {
  706. $handler->bindTo($this->data());
  707. return $handler($value);
  708. }
  709. return $value;
  710. }
  711. /**
  712. * Format the field attributes.
  713. *
  714. * @return string
  715. */
  716. protected function formatAttributes()
  717. {
  718. $html = [];
  719. foreach ($this->attributes as $name => $value) {
  720. $html[] = $name.'="'.e($value).'"';
  721. }
  722. return implode(' ', $html);
  723. }
  724. /**
  725. * @return $this
  726. */
  727. public function disableHorizontal()
  728. {
  729. $this->horizontal = false;
  730. return $this;
  731. }
  732. /**
  733. * @return array
  734. */
  735. public function getViewElementClasses()
  736. {
  737. if ($this->horizontal) {
  738. return [
  739. 'label' => "col-md-{$this->width['label']} {$this->getLabelClass()} text-capitalize",
  740. 'field' => "col-md-{$this->width['field']}",
  741. 'form-group' => 'form-group row form-field',
  742. ];
  743. }
  744. return ['label' => $this->getLabelClass().' text-capitalize', 'field' => '', 'form-group' => 'form-field'];
  745. }
  746. /**
  747. * Set element class.
  748. *
  749. * @param string|array $class
  750. *
  751. * @return $this
  752. */
  753. public function setElementClass($class)
  754. {
  755. $this->elementClass = array_merge($this->elementClass, (array) $class);
  756. return $this;
  757. }
  758. /**
  759. * Get element class.
  760. *
  761. * @return array
  762. */
  763. public function getElementClass()
  764. {
  765. if (! $this->elementClass) {
  766. $name = $this->elementName ?: $this->formatName($this->column);
  767. $this->elementClass = (array) str_replace(['[', ']'], '_', $name);
  768. }
  769. return $this->elementClass;
  770. }
  771. /**
  772. * Get element class string.
  773. *
  774. * @return mixed
  775. */
  776. protected function getElementClassString()
  777. {
  778. $elementClass = $this->getElementClass();
  779. if (Arr::isAssoc($elementClass)) {
  780. $classes = [];
  781. foreach ($elementClass as $index => $class) {
  782. $classes[$index] = is_array($class) ? implode(' ', $class) : $class;
  783. }
  784. return $classes;
  785. }
  786. return implode(' ', $elementClass);
  787. }
  788. /**
  789. * Get element class selector.
  790. *
  791. * @return string|array
  792. */
  793. protected function getElementClassSelector()
  794. {
  795. $elementClass = $this->getElementClass();
  796. $formId = $this->getFormElementId();
  797. $formId = $formId ? '#'.$formId : '';
  798. if (Arr::isAssoc($elementClass)) {
  799. $classes = [];
  800. foreach ($elementClass as $index => $class) {
  801. $classes[$index] = $formId.' .'.(is_array($class) ? implode('.', $class) : $class);
  802. }
  803. return $classes;
  804. }
  805. return $formId.' .'.implode('.', $elementClass);
  806. }
  807. /**
  808. * @return $this
  809. */
  810. public function hideInDialog()
  811. {
  812. if (
  813. $this->form instanceof Form
  814. && $this->form->inDialog()
  815. ) {
  816. $this->display(false);
  817. }
  818. return $this;
  819. }
  820. /**
  821. * @return string|null
  822. */
  823. protected function getFormElementId()
  824. {
  825. return $this->form ? $this->form->getElementId() : null;
  826. }
  827. /**
  828. * Add the element class.
  829. *
  830. * @param $class
  831. *
  832. * @return $this
  833. */
  834. public function addElementClass($class)
  835. {
  836. $this->elementClass = array_unique(
  837. array_merge($this->elementClass, (array) $class)
  838. );
  839. return $this;
  840. }
  841. /**
  842. * Remove element class.
  843. *
  844. * @param $class
  845. *
  846. * @return $this
  847. */
  848. public function removeElementClass($class)
  849. {
  850. $delClass = [];
  851. if (is_string($class) || is_array($class)) {
  852. $delClass = (array) $class;
  853. }
  854. foreach ($delClass as $del) {
  855. if (($key = array_search($del, $this->elementClass))) {
  856. unset($this->elementClass[$key]);
  857. }
  858. }
  859. return $this;
  860. }
  861. /**
  862. * Add variables to field view.
  863. *
  864. * @param array $variables
  865. *
  866. * @return $this
  867. */
  868. protected function addVariables(array $variables = [])
  869. {
  870. $this->variables = array_merge($this->variables, $variables);
  871. return $this;
  872. }
  873. /**
  874. * @param array|string $labelClass
  875. * @param bool $append
  876. *
  877. * @return $this|string
  878. */
  879. public function setLabelClass($labelClass, bool $append = true)
  880. {
  881. $this->labelClass = $append
  882. ? array_unique(array_merge($this->labelClass, (array) $labelClass))
  883. : (array) $labelClass;
  884. return $this;
  885. }
  886. /**
  887. * @return string
  888. */
  889. public function getLabelClass()
  890. {
  891. return implode(' ', $this->labelClass);
  892. }
  893. /**
  894. * Get the view variables of this field.
  895. *
  896. * @return array
  897. */
  898. public function variables()
  899. {
  900. return array_merge($this->variables, [
  901. 'id' => $this->id,
  902. 'name' => $this->getElementName(),
  903. 'help' => $this->help,
  904. 'class' => $this->getElementClassString(),
  905. 'value' => $this->value(),
  906. 'label' => $this->label,
  907. 'viewClass' => $this->getViewElementClasses(),
  908. 'column' => $this->column,
  909. 'errorKey' => $this->getErrorKey(),
  910. 'attributes' => $this->formatAttributes(),
  911. 'placeholder' => $this->placeholder(),
  912. 'disabled' => $this->attributes['disabled'] ?? false,
  913. 'formId' => $this->getFormElementId(),
  914. ]);
  915. }
  916. /**
  917. * Get view of this field.
  918. *
  919. * @return string
  920. */
  921. public function view()
  922. {
  923. return $this->view ?: 'admin::form.'.strtolower(class_basename(static::class));
  924. }
  925. /**
  926. * Set view of current field.
  927. *
  928. * @return string
  929. */
  930. public function setView($view)
  931. {
  932. $this->view = $view;
  933. return $this;
  934. }
  935. /**
  936. * Get script of current field.
  937. *
  938. * @return string
  939. */
  940. public function getScript()
  941. {
  942. return $this->script;
  943. }
  944. /**
  945. * Set script of current field.
  946. *
  947. * @return self
  948. */
  949. public function script($script)
  950. {
  951. $this->script = $script;
  952. return $this;
  953. }
  954. /**
  955. * To set this field should render or not.
  956. *
  957. * @return self
  958. */
  959. public function display(bool $display)
  960. {
  961. $this->display = $display;
  962. return $this;
  963. }
  964. /**
  965. * If this field should render.
  966. *
  967. * @return bool
  968. */
  969. protected function shouldRender()
  970. {
  971. return $this->display;
  972. }
  973. /**
  974. * Collect assets required by this field.
  975. */
  976. public static function collectAssets()
  977. {
  978. static::$js && Admin::js(static::$js);
  979. static::$css && Admin::css(static::$css);
  980. }
  981. /**
  982. * Render this filed.
  983. *
  984. * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View|string
  985. */
  986. public function render()
  987. {
  988. if (! $this->shouldRender()) {
  989. return '';
  990. }
  991. Admin::script($this->script);
  992. return view($this->view(), $this->variables());
  993. }
  994. /**
  995. * @return string
  996. */
  997. public function __toString()
  998. {
  999. $view = $this->render();
  1000. return $view instanceof Renderable ? $view->render() : (string) $view;
  1001. }
  1002. }