I am trying to display a image in a custom html view, for the new block editor.
But I am only getting the udi from the block.data.image Anyone have any ideas?
To achieve this you need to add a custom angularJS controller to your custom view, where you can then use the mediaResource to retrieve details of the media item from the Udi.
So first off we need to tell Umbraco to load the custom AngularJS controller, create a folder in your /App_Plugins/ folder for your custom block eg
/App_Plugins/CustomBlock/
in here create a file called package.manifest
(Umbraco looks for all package.manifest files in the app_plugins folder during startup, and loads any resources they specify)
in your package.manifest add a reference to your custom controller eg
Now create your customBlock.controller.js file in your CustomBlock folder, and add the following angular controller definition, note how we 'inject' the mediaResource service:
angular.module("umbraco").controller("customBlockController", function ($scope, mediaResource) {
//your property is called image so the following will contain the udi:
var imageUdi = $scope.block.data.image;
//the mediaResource has a getById method:
mediaResource.getById(imageUdi)
.then(function (media) {
console.log(media);
//set a property on the 'scope' for the returned media object
$scope.image = media;
});
});
Finally you need to wire up the custom view that you picked for your block with this angular controller, which you can do using the ng-controller attribute eg:
Thank you so much Marc! You are a life saver!
But is this really how the block editor is suppose to work? It feels a bit "unfinished" by having to create a controller for it ect.
Adding the controller in just gives you the flexibilty to do 'anything'.
Umbraco tends to have the philosophy of releasing things when they are 'improvements' to what currently exists, rather than final features that have implemented every scenario... which can feel frustrating sometimes, but also is really efficient, as it's only when people use things in the real world that the feedback of what is 'next' becomes obvious!
I'm sure it will evolve further as people begin to use it, but it would be good feedback to add to the issue tracker, that having a bunch of block editor preview 'helpers' to achieve common tasks, eg fetching an image from a udi... would be really useful and prevent everyone writing the same code over and over!
Image in custom block editor view
Hi!
I am trying to display a image in a custom html view, for the new block editor. But I am only getting the udi from the
block.data.image
Anyone have any ideas?//Johannes
Hi Johannes
To achieve this you need to add a custom angularJS controller to your custom view, where you can then use the mediaResource to retrieve details of the media item from the Udi.
So first off we need to tell Umbraco to load the custom AngularJS controller, create a folder in your /App_Plugins/ folder for your custom block eg
/App_Plugins/CustomBlock/
in here create a file called package.manifest
(Umbraco looks for all package.manifest files in the app_plugins folder during startup, and loads any resources they specify)
in your package.manifest add a reference to your custom controller eg
(that is all you need)
Now create your
customBlock.controller.js
file in your CustomBlock folder, and add the following angular controller definition, note how we 'inject' the mediaResource service:Finally you need to wire up the custom view that you picked for your block with this angular controller, which you can do using the ng-controller attribute eg:
That's the gist of it anyway!
regards
Marc
Thank you so much Marc! You are a life saver! But is this really how the block editor is suppose to work? It feels a bit "unfinished" by having to create a controller for it ect.
//Johannes
Hi Johannes
Don't worry.. nothing is ever "finished"!
Adding the controller in just gives you the flexibilty to do 'anything'.
Umbraco tends to have the philosophy of releasing things when they are 'improvements' to what currently exists, rather than final features that have implemented every scenario... which can feel frustrating sometimes, but also is really efficient, as it's only when people use things in the real world that the feedback of what is 'next' becomes obvious!
I'm sure it will evolve further as people begin to use it, but it would be good feedback to add to the issue tracker, that having a bunch of block editor preview 'helpers' to achieve common tasks, eg fetching an image from a udi... would be really useful and prevent everyone writing the same code over and over!
regards
Marc
regards
Marc
is working on a reply...
This forum is in read-only mode while we transition to the new forum.
You can continue this topic on the new forum by tapping the "Continue discussion" link below.