use Backpack\PageManager\app\Http\Controllers\Admin\PageCrudController;
use Cubist\Backpack\app\Template\TemplateAbstract;
-use Cubist\Backpack\app\Magic\CubistCrud;
use Illuminate\Support\Str;
class CubistPageCrudController extends PageCrudController
{
- use CubistCrud;
protected static $_templates = null;
/**
--- /dev/null
+<?php
+
+
+namespace Cubist\Backpack\app\Magic;
+
+
+use Cubist\Backpack\app\Magic\Fields\Field;
+
+trait BunchOfFields
+{
+ /**
+ * @var Field[]
+ */
+ protected $_fields = [];
+
+ /**
+ * @param $attributes
+ * @return Field
+ * @throws \Exception
+ */
+ public function addField(array $attributes)
+ {
+ $field = Field::getInstance($attributes);
+ $this->_fields[$field->getAttribute('name')] = $field;
+
+ return $field;
+ }
+
+ /**
+ * @return Field[]
+ */
+ public function getFields()
+ {
+ return $this->_fields;
+ }
+}
--- /dev/null
+<?php
+
+
+namespace Cubist\Backpack\app\Magic\Fields;
+
+class BunchOfFields extends Field
+{
+ use \Cubist\Backpack\app\Magic\BunchOfFields;
+
+ public function getDefaultAttributes()
+ {
+ return array_merge(parent::getDefaultAttributes(), ['bunch' => []]);
+ }
+
+ public function _postSetAttributes()
+ {
+ parent::_postSetAttributes();
+ $this->_setBunchOfFields();
+ }
+
+ protected function _setBunchOfFields()
+ {
+ $bunch = $this->getAttribute('bunch');
+ if (is_array($bunch)) {
+ foreach ($bunch as $item) {
+ if (is_array($item)) {
+ $f = Field::getInstance($item);
+ } else if ($item instanceof Field) {
+ $f = $item;
+ }
+ }
+ $this->_fields[$f->getAttribute('name')] = $f;
+ }
+ if (is_string($bunch)) {
+ if (class_exists($bunch)) {
+ $bunchInstance = new $bunch();
+ if (method_exists($bunchInstance, 'getFields')) {
+ $this->_fields = $bunchInstance->getFields();
+ }
+ }
+ }
+ }
+}
namespace Cubist\Backpack\app\Magic\Models;
use Backpack\CRUD\CrudTrait;
+use Cubist\Backpack\app\Magic\BunchOfFields;
use Cubist\Backpack\app\Magic\Fields\Field;
use Cubist\Backpack\app\Magic\Util;
use Backpack\CRUD\ModelTraits\SpatieTranslatable\Sluggable;
use HasTranslations;
use RevisionableTrait;
use HasMediaTrait;
+ use BunchOfFields {
+ addField as protected bunchAddField;
+ }
+
protected static $_doctrineTypesMapping = ['int' => 'integer'];
public $timestamps = true;
public $clonable = true;
- /**
- * @var Field[]
- */
- protected $_fields = [];
-
/**
* @var array
*/
}
-
- public function onRetreived(){
+ public function onRetreived()
+ {
}
*/
public function addField($attributes)
{
- /** @var Field $field */
- $field = Field::getInstance($attributes);
+ $field = $this->bunchAddField($attributes);
$field->setModelInstance($this);
if (is_callable([$field, 'getRelationship']) && null !== $field->getRelationship()) {
$this->_addRelationship($field);
}
$name = $field->getAttribute('name');
- $this->_fields[$name] = $field;
if ($field->getAttribute('fake', false) === true) {
$store_in = $field->getAttribute('store_in');
}
-
-
}
/**
namespace Cubist\Backpack\app\Template;
use Backpack\CRUD\CrudPanel;
+use Cubist\Backpack\app\Magic\BunchOfFields;
+use Cubist\Backpack\app\Magic\Fields\Field;
use Illuminate\Support\Str;
class TemplateAbstract
{
+ use BunchOfFields {
+ addField as protected bunchAddField;
+ }
+
/**
* @var CrudPanel
*/
$this->addField([
'name' => 'meta_title',
'label' => trans('backpack::pagemanager.meta_title'),
- 'type' => 'text',
+ 'type' => 'Text',
'hint' => trans('If empty, page title is used.') . ' ' . __('Recommended length: 60 chars'),
'tab' => 'Meta // SEO',
]);
$this->addField([
'name' => 'meta_description',
'label' => trans('backpack::pagemanager.meta_description'),
- 'type' => 'textarea',
+ 'type' => 'Textarea',
'hint' => __('Recommended length: 160 chars'),
'tab' => 'Meta // SEO',
]);
$this->addField([
'name' => 'robots',
'label' => __('Allow page index by search engines'),
- 'type' => 'checkbox',
+ 'type' => 'Checkbox',
'default' => true,
'tab' => 'Meta // SEO',
]);
public function init()
{
-
-
$this->_common();
}
$this->addField([
'name' => 'name',
'label' => trans('backpack::pagemanager.page_name'),
- 'type' => 'text',
+ 'type' => 'Text',
'wrapperAttributes' => [
'class' => 'form-group col-md-6',
],
$this->addField([
'name' => 'title',
'label' => trans('backpack::pagemanager.page_title'),
- 'type' => 'text',
+ 'type' => 'Text',
'fake' => false,
'tab' => 'General',
// 'disabled' => 'disabled'
$this->addField([
'name' => 'slug',
'label' => trans('backpack::pagemanager.page_slug'),
- 'type' => 'text',
+ 'type' => 'Slug',
'hint' => trans('backpack::pagemanager.page_slug_hint'),
'fake' => false,
'tab' => 'General',
$this->addField([
'name' => 'status',
- 'type' => 'select_from_array',
+ 'type' => 'SelectFromArray',
'default' => '0',
'label' => __('Status'),
'options' => ['0' => __('Offline'), '1' => __('Published')],
}
/**
- * Add a field to the create/update form or both.
- *
- * @param string|array $field The new field.
- * @param string $form The CRUD form. Can be 'create', 'update' or 'both'. Default is 'both'.
- *
- * @return CrudPanel
+ * @param array $attributes
+ * @return Field
+ * @throws \Exception
*/
- public function addField($field, $form = 'both')
+ public function addField(array $attributes)
{
// Set default options of field
$defaults = ['tab' => 'Contents',
'fake' => true,
- 'store_in' => 'extras'];
- $field = array_merge($defaults, $field);
+ 'store_in' => 'extras',
+ 'form' => 'both'];
+ $attributes = array_merge($defaults, $attributes);
+
+ $field = $this->bunchAddField($attributes);
+ $this->crud->addField($field->getDefinition(), $field->getAttribute('form'));
- return $this->crud->addField($field, $form);
+ return $field;
}
}