How to create custom JSON-API resource drupal 8 - drupal

I have issues with creating custom JSON API resource which will be visible on mydrupalsite/jsonapi link.
I have found this example:
https://glamanate.com/blog/using-json-api-query-your-search-api-indexes
which shows how to create custom resource but it is outdated. I tried to make a custom module looking in this article, but it doesn't work anymore because json-api module has changed.
For example, I want to show all nodes and make the jsonapi endpoint of type nodes--all which will serve all the nodes of my Drupal site.
I am using drupal 8.7.1 and JSON API core module.
I have tried to google this problem, but there are no examples for creating custom json api endpoints.
For final result i want to make jsonapi endpoint for example: mydrupalsite/jsonapi/nodes/all which will serve all nodes.

You could declare REST endpoints within a custom module.
In /modules/custom/mymodule/src/Plugin/rest/resrouce/MyEndPoints.php, create a class extending ResourceBase, according to https://www.drupal.org/docs/8/api/restful-web-services-api/custom-rest-resources.
Then you could define which set of nodes you want to expose, use query parameters to filter them, paginate, or do whatever you want.
See also : https://medium.com/#emerico/create-custom-rest-resource-for-get-and-post-method-in-drupal-8-e445330be3ff
I successfully used this technique to get events displayed with FullCalendar library, I can elaborate on this if it helps.
Good luck with it

The module does not currently support external connection to its internal-use-only API according to the JSON:API's module maintainers, hence why your referenced module does not work anymore.
There are plans for the mixed bundle you are requesting. See the thread below for the discussion.
https://www.drupal.org/project/jsonapi_extras/issues/2956414

Checkout JSON:API Cross Bundles
We discussed this in Decoupled Days 2019. We came to the following
conclussions:
We will put this under a different contrib as per my request ☺️ jsonapi_cross_bundles
There will only be support for GET
The edge cases in filtering will be solved using whatever stance Entity Query API uses.
In parallel we'll create a JSON:API issue to add a payload to the 404 routes like /jsonapi/node that will contain: (a) a list of links
to the resource types for that entity type, and (b) link to a
documentation page explaining the cross bundle situation (maybe even
mentioning the experimental jsonapi_cross_bundles module).
Source: https://www.drupal.org/project/jsonapi_extras/issues/2956414#comment-13189872

As of 2021, the best way to do this is the JSON:API Resources module.
This module allows you to add your own JSON:API endpoints in a custom module.
There are several examples of how to define your own resources.

Related

Alfresco CMIS: Retrieving relevant context when searching for content

I am using Alfresco Community 201707 x64. My application is querying the repository via CMIS, using Apache Chemistry Open CMIS 1.1.
The question, as the title suggests, is: when querying the repository for document content (using the CONTAINS predicate on the search query), how do I retrieve a document's relevant context? Can I do it using CMIS, or do I have to use a different approach?
For example, in Alfresco Share, when a user searches the repository for documents containing some word, the relevant results are displayed with a snippet of the document's content with said word highlighted.
I do not think this is available in CMIS, it is however generally available if using SOLR, depending on Alfresco version.
http://docs.alfresco.com/5.2/concepts/search-api-highlight.html
As said by #Lista,
It is a new feature coming with Alfresco 5.2.1 : http://docs.alfresco.com/5.2/concepts/search-api-highlight.html
You can use it by making a call to the /search endpoint :
The Search API provides access to the search features of Alfresco
Content Services.
The Search API accepts POST requests containing JSON structures as
described in the table below. The JSON is structured to group options
related to different query concepts together.
The Search API is defined under the search namespace. It uses the
/search endpoint, which does not accept any URL parameters and is
therefore, completely controlled via the parameters in a POST body.
But as far as I know, this is not possible to do it by CMIS. Can you switch the way you are interacting with Alfresco (the REST way) ?

Embed Alfresco WebPreviewer in my own website

I have a Spring MVC application that connect with Alfresco using CMIS libraries, actually I can upload documents and download it but I need integrate Alfresco's WebPreviewer to preview documents in my app.
I found some code here but I don't know how to do it
It's hard to say for certain because of the limited amount of information that you've provided, but I think that the problem that you're going to be faced with when trying to use any of the existing previewer code is one of authentication. If you're using only using CMIS then you won't be able to use any of the WebScript based REST APIs that the Alfresco widgets will be using.
There are two possible previewer widgets that you can use - the older YUI2 based previewer (that you'd currently find in the document details page and the Document Library film strip view, and the newer Aikau component that you'd find in the faceted search previewer (from version 5.0 onwards).
I suspect that you won't be able to re-use either of these components without either authenticating against Alfresco in a way that allows you to access the WebScript based REST APIs or extending and customizing those widgets.
You've said that you have your own Spring MVC application, but you haven't said whether or not that is using the Surf extension - if it is, and you're using the authentication capabilities provided by Surf then you will be authenticated to use those REST APIs - as the Surf authentication provides access across all APIs (including CMIS) via a single authentication.
If you are able to access those APIs then you should be able to follow the steps outlined in both the form post and the blog posts in your own question and the previous answer, however - based on your question I suspect that you can't do that.
If you've not come across it, you might be interested in the Aikau archetype that builds a ready-made Alfresco client using Surf (see this link) and that tutorial also shows how to use the Aikau previewer (see here).
Because this is providing you with a Spring MVC client that is preconfigured to authenticate against Alfresco, you might be able to port your application to use it.
Otherwise, as I said earlier - chances are you'll need to extend the existing widgets to use the CMIS APIs to render the previewers. Again, Aikau is easier to extend that the old YUI2 widgets - but is reliant on Surf.

How to get and set custom metadata values in Alfresco via REST API?

Please help me to get/set metadata by REST API Calls.I am Using Alfresco 5.0 version.
Unfortunately the Alfresco REST API is limited to a small subset wich doesn't support (custom) metadata. Alfresco expects you use CMIS instead. Since CMIS may be too complicated / an overkill to integrate in your use case you could still create your own WebScripts to enable RESTful methods as you expect. This is straight forward. Take a look into the Web_Scripts_Examples and the official Web Scripts Documentation
You can find details of all existing rest api on below link
http://host:port/alfresco/service/index/all
If the api does not exist on above than you need to create your custom one.I am not sure about metadata.But you can find it on above link.

Cross linking applications in Symfony 2

There are a few tutorials on the web regarding the multi apps structure with Symfony 2, however I haven't seen any post on cross linking the apps (in term of http urls, how do you generate a link to the frontend blog post from your backend application?)
It's possible to put all the shared routes in a shared bundle, but then how do we generate the apps with the correct application path applied to it? For example, in my case I need to generate mysite.com/reset_password but since I do it from the backend I always get mysite.com/backend/reset_password. Of course I can trim the /backend/ out manually but that does not seem like the way to do it.
PS: I understand the downside of the multiple apps structure, however there are reason we want to use it.
As far as I know there is no built in way to do that, but it doesn't sound especially difficult to do.
Create a web service on the site you want to link to which provides URLs to specific locations. You can then normalize these URLs to generic keys. Then create an internal service to build a URL from this data.

Question about using remoteobject in modules

I have a number of modules in my flex application and in each module I use a remoteobject to retrieve dynamic data from the server related to each module. Recently I read a couple of comments in some blogs people saying that remoteobjects should not be used in modules, instead they should be in the main application. Is this true and why? And if it's true, what would I do with the resulthandler in the main app, since the code is very specific to each module, there's a lot of data manipulation and code setting values of components in the module, how would the handler in the main app access the components inside the module?
Thanks
It seems ridiculous to me to avoid using RemoteObject in a module. The whole purpose of a Module is to be a self contained portion of an application. And there is no reason why remote service calls can't be part of the self contained piece.
Do you have links to blog posts making these claims? I'm sure any such claims would be context specific (as are most best practices).

Resources