protected function CronHandler::createMediaEntity in Instagram Feeds 8
Creates Media entity with Instagram data.
Parameters
array $post: Instagram post data array.
\Drupal\instagram_feeds\Entity\InstagramAccountInterface $account: Instagram Feed configuration Entity.
1 call to CronHandler::createMediaEntity()
- CronHandler::processAccount in src/
CronHandler.php - Instagram posts import processor for the given account.
File
- src/
CronHandler.php, line 217
Class
- CronHandler
- Instagram Feeds Cron Handler Service.
Namespace
Drupal\instagram_feedsCode
protected function createMediaEntity(array $post, InstagramAccountInterface $account) {
$media_type = $account
->getMediaType();
$mapping = $this->config
->get('mapping.' . $media_type);
$source_plugin = $this
->getInstagramSourcePluginID($media_type);
$entity_array = [
'bundle' => $media_type,
];
switch ($source_plugin) {
case 'image':
$image = system_retrieve_file($post['media_url'], NULL, TRUE);
$entity_array[$this
->getInstagramSourceField($media_type)] = [
'target_id' => $image
->id(),
];
break;
case 'instagram':
$entity_array[$this
->getInstagramSourceField($media_type)] = $post['permalink'];
break;
}
foreach ($mapping as $entity_field_name => $post_field_name) {
if ($entity_field_name == 'name') {
$token_data = [
'instagram_account' => $account,
'instagram_post' => $post,
];
$entity_array['name'] = empty($post_field_name) ? $account
->label() . ' (' . date('m/d/Y', $post['timestamp']) . ')' : $this
->token()
->replace($post_field_name, $token_data, [
'clear' => TRUE,
]);
$entity_array['name'] = trim($entity_array['name']);
}
elseif ($post_field_name == 'tags' && $post[$post_field_name]) {
/** @var \Drupal\Core\Entity\EntityFieldManagerInterface $field_manager */
$fields = $this->entityFieldManager
->getFieldDefinitions('media', $media_type);
$field_definition = $fields[$entity_field_name];
$entity_array[$entity_field_name] = 'entity_reference' == $field_definition
->getType() ? $this
->getTerms($field_definition, $post['tags']) : $post['tags'];
}
elseif (!empty($post_field_name) && $post[$post_field_name]) {
$entity_array[$entity_field_name] = $post[$post_field_name];
}
}
$media_entity = $this->mediaStorage
->create($entity_array);
// Dispatch an event, so other modules can modify media entity before save.
$event = new MediaEntityInstantiatedEvent($this->config, $account, $media_entity, $post);
$this->eventDispatcher
->dispatch(MediaEntityInstantiatedEvent::getEventName(), $event);
$event->mediaEntity
->save();
}