Field.php 23 KB

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