/**
  * @inheritdoc
  */
 protected function initDefaultButtons()
 {
     $entity = Yii::$app->getRequest()->getQueryParam('entity', null);
     $primaryKey = AdminHelper::getEntity($entity)->primaryKey();
     if (!isset($this->buttons['view'])) {
         $this->buttons['view'] = function ($url, $model, $key) use($entity, $primaryKey) {
             $options = array_merge(['title' => Yii::t('admin', 'View'), 'aria-label' => Yii::t('admin', 'View'), 'data-pjax' => '0'], $this->buttonOptions);
             Html::addCssClass($options, 'btn btn-primary');
             return Html::a(Yii::t('admin', 'View'), ['manage/view', 'entity' => $entity, 'id' => $model->{$primaryKey}], $options);
         };
     }
     if (!isset($this->buttons['update'])) {
         $this->buttons['update'] = function ($url, $model, $key) use($entity, $primaryKey) {
             $options = array_merge(['title' => Yii::t('admin', 'Edit'), 'aria-label' => Yii::t('admin', 'Edit'), 'data-pjax' => '0'], $this->buttonOptions);
             Html::addCssClass($options, 'btn btn-warning');
             return Html::a(Yii::t('admin', 'Edit'), ['manage/update', 'entity' => $entity, 'id' => $model->{$primaryKey}], $options);
         };
     }
     if (!isset($this->buttons['delete'])) {
         $this->buttons['delete'] = function ($url, $model, $key) use($entity, $primaryKey) {
             $options = array_merge(['title' => Yii::t('admin', 'Delete'), 'aria-label' => Yii::t('admin', 'Delete'), 'data' => ['confirm' => Yii::t('admin', 'Are you sure you want to delete this item?'), 'method' => 'post', 'pjax' => '0']], $this->buttonOptions);
             Html::addCssClass($options, 'btn btn-danger');
             return Html::a(Yii::t('admin', 'Delete'), ['manage/delete', 'entity' => $entity, 'id' => $model->{$primaryKey}], $options);
         };
     }
 }
Exemplo n.º 2
0
 /**
  * @inheritdoc
  */
 public function init()
 {
     if (is_callable($this->query)) {
         $this->query = call_user_func($this->query, $this->model);
     }
     if ($this->query instanceof ActiveQueryInterface) {
         if (!$this->labelAttribute) {
             throw new InvalidConfigException('Parameter "labelAttribute" is required');
         }
         $this->items = $this->query->all();
         foreach ($this->items as $i => $model) {
             $this->items[$i] = AdminHelper::resolveAttribute($this->labelAttribute, $model);
         }
     }
     if ($this->allowEmpty) {
         $this->items = ArrayHelper::merge([null => Yii::t('yii', '(not set)')], $this->items);
     }
     parent::init();
 }
Exemplo n.º 3
0
 /**
  * Format $value as [[ActiveRecord]] model.
  *
  * ```php
  * [
  *     ...
  *     'format' => ['model', ['labelAttribute' => 'username']],
  *     ...
  * ]
  * ```
  *
  * @param ActiveRecord|ActiveRecord[] $value
  * @param array $options 'labelAttribute' is a related model's attribute to be shown as field value
  * @return string
  */
 public function asModel($value, $options = ['labelAttribute' => 'id'])
 {
     if (!$value) {
         return $this->nullDisplay;
     }
     if (is_array($value)) {
         $values = [];
         foreach ($value as $i => $v) {
             $values[$i] = $this->asModel($v, $options);
         }
         return implode(', ', $values);
     }
     $label = $value->getAttribute($options['labelAttribute']);
     $item = AdminHelper::getEntity($value->className());
     if ($item !== null) {
         return Html::a($label, ['view', 'item' => $item->id, 'id' => $value->primaryKey]);
     }
     return $label;
 }