Create drupal content programmatically with custom entity ( assets )

Solutions

this is the right code:

function create_assets(){

$asset = entity_create('asset' , array('type' =>'image'));
$asset->title = 'image publication';
//$asset->field_asset_image = array(LANGUAGE_NONE => array(0 => array('value' => get_external_image("http://cdn.sstatic.net/drupal/img/logo.png"))));
$asset->field_alt["und"][0]["value"] = "alt image publication";
//$asset->save();
entity_save('asset', $asset);
}

I think you can try to use core and EntityAPI methods for this task like this:

$asset = entity_get_controller('asset');
$asset->type = 'MY_TYPE';
$asset->... = '...';
....

$asset->save();
Tags: Media

Similar questions

Get path to theme assets in Twig template
I have a question about getting a path to an image in a Twig template. The image is not assigned to a field or something. Just a static image which is stored in "MYTHEME/image/icon/my-icon.png". In Drupal 7 I get the path in my node.template with the following code: How does it work with in Drupal 8? I tried to pass a variable in template_preproces...
How do I include JavaScript with assets (libraries.yml)?
This is my mytheme.libraries.yml file: This is my js/effect.js file: I created jquery.js in core folder of my site root. Even if I remove the dependency line from my libraries.yml and remove jquery syntax from my effect.js, I receive nothing on my site.
How to load jQuery assets before blocks and regions
I need a jQuery script to load immediately before a certain block shows on my Drupal 8 front page. I'm trying to use the following but I get 'TypeError $ is not a function' in code inspector. Here's my script: The problem is the first line. In Drupal 8 what is the preferred way to write this script to accomplish what I want?
How to modify .htaccess to serve brotli compressed static assets over gziped ones?
I'm trying to use brotli compressed static assets with Advanced CSS/JS Aggregation as detailed here, https://www.drupal.org/node/2773807. The assets are created as they should be, but they're never served because the .htaccess file doesn't know to do that. lyncd.com has a short walkthrough, https://lyncd.com/2015/11/brotli-support-apache/, but I ha...
Can't add assets to my Bootstrap sub-theme
I'm currently struggling to include *.css and *.js files to my Bootstrap sub-theme. I use Drupal 8 and the Bootstrap 3 base theme. I followed the instructions of the official documentation and went to create a custom SASS sub-theme for my site. I could quickly use my newly created sub-theme in the admin panel. Great. However it seems none of the as...
Random page rendering error: A stray renderRoot() invocation is causing bubbling of attached assets to break
I have a D8 site that's in development and I am getting this error thrown at random on pages which most of the time render quite happily. There doesn't seem to be any real pattern in it. I can see that it looks like it's designed to prevent the renderer creating an infinite loop (the code is on line 133 of core/lib/Drupal/Core/Render/Renderer.php),...

Also ask

We use cookies to deliver the best possible experience on our website. By continuing to use this site, accepting or closing this box, you consent to our use of cookies. To learn more, visit our privacy policy.