/** * @param MonsterProvidersTrait $entity * * @return int|string */ public static function ensureStaticProvider(&$entity) { $providers = $entity->getEntityDataProviders(); foreach ($providers as $index => $provider) { /** @var array $provider */ $className = ArrayHelper::getValue($provider, 'class', null); if ($className === StaticContentProvider::class) { return $index; } } $providers[uniqid('prov', true)] = ['class' => StaticContentProvider::class, 'entities' => []]; $entity->setEntityDataProviders($providers); return key(array_slice($providers, -1, 1, true)); }
/** * @param MonsterProvidersTrait|yii\base\Model $model * @param string $postKey */ protected function providersSupplied(&$model, $postKey) { if ($this->action() === self::ACTION_DEFAULT) { // skip on default action return; } $providers = ArrayHelper::getValue($this->visualBuilderProvided(), "{$postKey}.providers", null); if (is_array($providers) === false) { // no providers supplied return; } //! @todo add check for proper class names and properties here $model->setEntityDataProviders($providers); if ($this->action() === self::ACTION_SAVE) { $result = $model->hasMethod('saveMonsterContent') ? $model->saveMonsterContent() : $model->saveProviders(); if ($result === false) { throw new \Exception(var_export($model->errors, true)); } } }