You are here

public static function Asset::fromValues in Mini site 8

Instantiate class from an array of values.

Parameters

array $values: Array of values to instantiate the field.

Return value

\Drupal\minisite\Asset An instance of the class.

Overrides AssetInterface::fromValues

5 calls to Asset::fromValues()
Asset::load in src/Asset.php
Load asset by id.
Asset::loadAll in src/Asset.php
Load all assets.
Asset::loadByAlias in src/Asset.php
Load asset by alias.
Asset::loadByUri in src/Asset.php
Load asset by URI location.
AssetTest::testAssetFromValues in tests/src/Functional/AssetTest.php
Test Asset::fromValues().

File

src/Asset.php, line 136

Class

Asset
Class Asset.

Namespace

Drupal\minisite

Code

public static function fromValues(array $values) {
  $required_fields = [
    'entity_type',
    'entity_bundle',
    'entity_id',
    'entity_language',
    'field_name',
    'source',
  ];
  if (count(array_diff_key(array_flip($required_fields), array_filter($values))) > 0) {
    throw new AssetException('Unable to instantiate Asset instance from the provided values as required values are missing');
  }
  $values += [
    'filemime' => NULL,
    'filesize' => NULL,
  ];
  $instance = new self($values['entity_type'], $values['entity_bundle'], $values['entity_id'], $values['entity_language'], $values['field_name'], $values['source'], $values['filemime'], $values['filesize']);
  if (!empty($values['id'])) {
    $instance
      ->setId($values['id']);
  }
  if (!empty($values['alias'])) {
    $instance
      ->setAlias($values['alias']);
  }
  return $instance;
}