Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Include additional info in $entityinfo #10

Closed
p-a-s-c-a-l opened this issue Aug 7, 2019 · 20 comments
Closed

Include additional info in $entityinfo #10

p-a-s-c-a-l opened this issue Aug 7, 2019 · 20 comments
Assignees
Labels
BB: Scenario Management Scenario Management Building Block enhancement New feature or request

Comments

@p-a-s-c-a-l
Copy link
Member

p-a-s-c-a-l commented Aug 7, 2019

It would be helpful if we could include some additional properties from study group node in $entityinfo so that we can avoid some API calls in our external components.

ATM we would need

  • field_emikat_id
  • field_data_package: here just the data package entity uuid from data/id, e.g. a8ff7930-4a9f-4289-8246-3383ba13c30f
  • field_area: the valueproperty of the geofiled entity, e.g. POLYGON((14.149017 40.8268,14.149017 40.894051,14.316902 40.894051,14.316902 40.8268,14.149017 40.8268))
@p-a-s-c-a-l p-a-s-c-a-l added the enhancement New feature or request label Aug 7, 2019
@p-a-s-c-a-l p-a-s-c-a-l added this to the D1.3 CLARITY CSIS v1 milestone Aug 7, 2019
@p-a-s-c-a-l p-a-s-c-a-l added this to Backlog: High Priority in T1.3 Climate Services Co-creation via automation Aug 7, 2019
@p-a-s-c-a-l p-a-s-c-a-l changed the title Inlcude addtional info in $entityinfo Include additional info in $entityinfo Aug 7, 2019
@patrickkaleta
Copy link
Contributor

$entityinfo now contains the mentioned properties as described above:

  • study_emikat_id
  • study_datapackage_uuid
  • study_area

(If a value has not yet been set for the study, the field contains null)

@patrickkaleta
Copy link
Contributor

As requested here, $entityinfo now contains a eea_city_field entry with the EEA city name (null if no city has been yet selected in the study).

@p-a-s-c-a-l
Copy link
Member Author

In order to support this feature, we need additional properties in $entityinfo which relate to fields in Data Package and Resource node types, e.g. uuid and field_bounding_box of data_package.

I noticed there are two methods in csis_helpers.module which apparently do the same:
function csis_helpers_node_entity_info(\Drupal\Core\Entity\EntityInterface $entity) and
function csis_helpers_group_entity_info(\Drupal\Core\Entity\EntityInterface $entity). Why?

I want to extend csis_helpers_node_entity_info. How can I access e.g. the data_package node and extract the required fields?

p-a-s-c-a-l added a commit that referenced this issue Aug 26, 2019
@fgeyer16
Copy link

They do the same because we need it once done for a node entity and once for a group entity as the source of the data. Since the way how to get the data is different for node and group we have to do it twice in slightly different ways.

As far as I can see the the data package uuid is already included in the entityinfo.

"csisHelpers": {
"entityinfo": {
"step": null,
"step_uuid": null,
"study": "17",
"study_uuid": "40e0c559-a1e6-4724-af66-bf2df1f3d7ee",
"study_emikat_id": "3179",
"study_datapackage_uuid": "2434ce93-93d4-4ca2-8618-a2de768d3f16",
"study_area": "POLYGON ((14.133408 40.838424, 14.133408 40.897965, 14.243305 40.897965, 14.243305 40.838424, 14.133408 40.838424))",
"eea_city_name": "Napoli",
"write_permissions": 1
}
},

So you could load the data package by REST API and extract the properties then. If you want to have the properties direct in the entity info you can extract them in the module in the

if ($groupDatapackageID) {
// load the datapackage selecteds by the study
$datapackageNode = \Drupal\node\Entity\Node::load($groupDatapackageID);
$datapackageUUID = $datapackageNode->uuid();
}

blocks in the functions you mentioned (don't forget to do it in both) and then add the value to the

$entityinfo = array(
'step'=> null,
'step_uuid' => null,
'study'=> $entity->id(),
'study_uuid'=> $entity->uuid(),
'study_emikat_id' => $entity->get('field_emikat_id')->value,
'study_datapackage_uuid' => $datapackageUUID,
'study_area' => $entity->get('field_area')->value,
'eea_city_name' => $eeaName,
'write_permissions' => ($has_user_special_roles ? 0 : 1)
);

array.

Extracting the value might be tricky. Always the same will be the beginning with
$variable = $datapackageNode->get('field_machine_name') for entity reference fields you will have to do $variable = $datapackageNode->get('field_machine_name')->target_id for other fields it is most likley $variable = $datapackageNode->get('field_machine_name')->value . If this does not give you the right values please make a kint($datapackageNode->get('field_machine_name')) and refresh a page (best do a cache clear) on which you need the info. This will print out the structure of the field in a nice good readable mode. Sometimes the Site is broken after printing out the debug info since this can be send before the html header. SO do not be concerned if this happens. The page will work as designed if you remove the line.

@p-a-s-c-a-l
Copy link
Member Author

p-a-s-c-a-l commented Aug 26, 2019

@fgeyer16 In the views I'm talking about, there is not study (group), so study_datapackage_uuid which is derived from field_data_package of the study group type is not available at all. E.g. on this page, how do I get the uuid of the datapackage?

p-a-s-c-a-l added a commit that referenced this issue Aug 26, 2019
@fgeyer16
Copy link

Ah I misunderstood you, sorry. So you want to use the entity info for other than gl-step nodes?
For this function csis_helpers_node_entity_info has to be modified.

First step is to determine which node type we have: change the current code of the function to

$type = $entity->bundle()
 switch ( $type) {
   case 'gl_step':
   /*the code of the current function here*/
  case: 'node_type'
 }

In the case part fort the data package node type you can then query the fields of the data package by using $variable = $datapackageNode->get('field_machine_name') as I described above.
For resources you have query the data packagee the resource belongs first. This works like this::

 $query = \Drupal::entityQuery('node')
   ->condition('type', 'data_package'),
   ->condition('field_resources', $entity->id());
 $nids = $query->execute();
 $nodes = \Drupal\node\Entity\Node::loadMultiple($nids);

Doing so throws some questions: What if the resource is in more than one datapackage? how to know which one is the correct data package?.

After you have done this you will have to modify

function csis_helpers_node_view(array &$build, EntityInterface $entity, EntityViewDisplayInterface $display, $view_mode){
  if ($entity->bundle() == "gl_step" && $view_mode != "compact") {
    $build['#attached']['drupalSettings']['csisHelpers']['entityinfo'] = csis_helpers_node_entity_info($entity);
    $build['#attached']['library'][] = 'csis_helpers/entityinfo_helpers';
    $build['#attached']['library'][] = 'csis_helpers/include_in_report';
    $build['#attached']['library'][] = 'csis_helpers/html2canvas';
    $build['#attached']['library'][] = 'csis_helpers/seamless';
    $build['#attached']['library'][] = 'csis_helpers/update_step_relations';
  }
}

Add a

if($entity->bundle() == "content type" ){}

Block for each content type and set in this

$build['#attached']['drupalSettings']['csisHelpers']['entityinfo'] = csis_helpers_node_entity_info($entity);

When do need this infos? Only if the data package is viewed? I ask because in current design, if you view a page with Study/Step and Datapackage the entitity info will only show the info from one of them. The one which is rendered last.
To circumvent this we will have to change

$build['#attached']['drupalSettings']['csisHelpers']['entityinfo'] = csis_helpers_node_entity_info($entity);

to something like

$build['#attached']['drupalSettings']['csisHelpers']['someotherkey'] = csis_helpers_node_entity_info($entity);

@p-a-s-c-a-l
Copy link
Member Author

Ok, thanks. I'll give it a try.

What if the resource is in more than one datapackage?

No that's not problem. In Data Package Nodes, I'm only interested in the data package uuid, in resource nodes, only the resource uuid.

@p-a-s-c-a-l p-a-s-c-a-l self-assigned this Aug 29, 2019
p-a-s-c-a-l added a commit that referenced this issue Aug 29, 2019
@p-a-s-c-a-l
Copy link
Member Author

p-a-s-c-a-l commented Aug 29, 2019

if you view a page with Study/Step and Datapackage the entitity info will only show the info from one of them. The one which is rendered last.

That's a problem. E.g. for this Data Package, $entityInfoseems the become "polluted" with information from a study the data package is assigned to. That means, from the information available in $entityInfo I cannot determine whether the page shown is a study, data package or a resource. :-(

@p-a-s-c-a-l
Copy link
Member Author

p-a-s-c-a-l commented Aug 30, 2019

I've spilt entityinfo in studyInfo, datapackageInfo and resourceInfo in /feature/010-extend-entityinfo. entityInfo should no longer be used.

BTW, spatial_extent' => $entity->get('field_spatial_extent')->value returns null for datapackage and resource even if a spatial extent is set.

p-a-s-c-a-l added a commit that referenced this issue Aug 30, 2019
@fgeyer16
Copy link

Splitting is what I wanted to suggest.

The field spatial extend is an entity reference field so we need the entity which is referenced and get the wanted field value from it.

$spatial_extends = $entity->get('field_spatial_extent')->referencedEntities(); // referencedEntities() returns an array of entities
$spatial_extend = $spatial_extend[0]->get('fieldname')->value;

@p-a-s-c-a-l
Copy link
Member Author

p-a-s-c-a-l commented Sep 2, 2019

Splitting is what I wanted to suggest.

Unfortunately, this doesn't work. studyInfo, datapackageInfo and resourceInfo, etc, always contain all properties of all entity types. I have the impression, that $entityinfo; is defined as global variable.

grafik

$spatial_extends = $entity->get('field_spatial_extent')->referencedEntities(); 
if(!empty($spatial_extends) && is_array($spatial_extends) && sizeof($spatial_extends) > 0)

This returns falseeven if field_spatial_extent is defined. Is there any possibility to debug the module? Printing some log messages may work, but I would prefer a professional debugger.

p-a-s-c-a-l added a commit that referenced this issue Sep 2, 2019
@p-a-s-c-a-l
Copy link
Member Author

Now, this statement seems to evaluate to false on the Data Package Node: /node/682

$type = $entity->bundle();

    if ($type == ("gl_step" || "data_package" || "data_package_metadata") && $view_mode != "compact") {

@p-a-s-c-a-l
Copy link
Member Author

csis_entity_info.js needs to be updated, too. What's the purpose of this script? Where is it used?

p-a-s-c-a-l added a commit that referenced this issue Sep 3, 2019
p-a-s-c-a-l added a commit that referenced this issue Sep 3, 2019
p-a-s-c-a-l added a commit that referenced this issue Sep 3, 2019
@patrickkaleta
Copy link
Contributor

csis_entity_info.js needs to be updated, too. What's the purpose of this script? Where is it used?

I was wondering about that myself. That's a question to @fgeyer16, do we even need this anymore?

@p-a-s-c-a-l :
regarding debugging the content of entites in Drupal I usually use dump($variable) (first you have to select the "Symfony var-dumper" option in the Devel settings (.../admin/config/development/devel) of your local instance).

So for example to check the content of the $spatial_extends it would look like this:
dump($spatial_extends);
and on the website it would then look something like this:
debugging-nodes

From that I learned that for the spatial extend the fields have different names than what @fgeyer16 wrote above. To extract the values, the code should look like this:

        $spatialExtent = array(
            'minx' => $spatial_extends[0]->get('field_xmin')->value,
            'miny' => $spatial_extends[0]->get('field_ymin')->value,
            'maxx' => $spatial_extends[0]->get('field_xmax')->value,
            'maxy' => $spatial_extends[0]->get('field_ymax')->value,
        );

In addition I deactivated all your console_log function calls and then it seemed to work just fine for your latest commit and viewing this data package.

Checking the drupal-settings in the html document for above data package, I found:
"datapackageInfo":

  • id
  • uuid
  • write_permissions
  • name
  • minx
  • miny
  • maxx
  • maxy

Something to keep in mind:
Since on that data package page all included resources are shown as "rendered entities", the resourceInfo is also called for each of those resources, however only one makes it into the drupal-settings, since they get overwritten each time. idk if you need the resourceInfo when viewing the data pacakge or not, but this is something we should keep in mind.

p-a-s-c-a-l added a commit that referenced this issue Sep 3, 2019
p-a-s-c-a-l added a commit that referenced this issue Sep 3, 2019
p-a-s-c-a-l added a commit that referenced this issue Sep 3, 2019
p-a-s-c-a-l added a commit that referenced this issue Sep 3, 2019
p-a-s-c-a-l added a commit that referenced this issue Sep 3, 2019
@fgeyer16
Copy link

fgeyer16 commented Sep 3, 2019

csis_entity_info.js needs to be updated, too. What's the purpose of this script? Where is it used?

This was thought to provide helper functions so nobody needs to know where in the drupal settings the entity infos are.If nobody uses this or wants this we can remove this file as well.

Regarding debugging: I use kint($variable). This is function/module provided by the devel module. You need to enable the devel Kint module. @patrickkaleta provides dump also information on available methods for objects?

Since on that data package page all included resources are shown as "rendered entities", the resourceInfo is also called for each of those resources, however only one makes it into the drupal-settings, since they get overwritten each time. idk if you need the resourceInfo when viewing the data pacakge or not, but this is something we should keep in mind.

I'm thinking if there is away to fill the entityinfos only if the node is not rendered because it is referenced by another one.

@patrickkaleta
Copy link
Contributor

@patrickkaleta provides dump also information on available methods for objects?

No it does not. However, I noticed that kint() sometimes fails to load an object if it's too big (which sadly is quite often the case for our entities) and then I get just a blank page.

@fgeyer16
Copy link

fgeyer16 commented Sep 3, 2019

I noticed that kint() sometimes fails to load an object if it's too big (which sadly is quite often the case for our entities) and then I get just a blank page.

yes that can happen because if you kint() the "right" variable it will load the page multiple times and then run out of memory especially if somewhere in the variable there is a reference to the(a) (sub)variable which can cause some kind of endless loop I assume.

In web/modules/contrib/devel/kint/kint/config.default.php you can change the "kint levels". This is how many levels kint will proceed to print out the stuff. Default ist set to 7 but this seems to be to much. In my experience 4 to 5 levels is enough and stops this error to occur. AN if you need to dig depper you will have to do a kint($variable['the']['deeper']['level'])

@fgeyer16
Copy link

fgeyer16 commented Sep 3, 2019

@p-a-s-c-a-l: You wait for me to solve that with the additional infos right? Not that we work on the same thing an may against each other as well ;-)

@p-a-s-c-a-l
Copy link
Member Author

@fgeyer16 No, finally I've managed implemented all changes I need in https://github.com/clarity-h2020/csis-helpers-module/tree/feature/010-extend-entityinfo, now testing with iFrame Map Components. If this work's I'll merge the branch in DEV. So ATM no action from your side needed. Thanks!

p-a-s-c-a-l added a commit that referenced this issue Sep 5, 2019
@p-a-s-c-a-l
Copy link
Member Author

done

T1.3 Climate Services Co-creation automation moved this from Backlog: Medium Priority to Done Sep 6, 2019
p-a-s-c-a-l added a commit that referenced this issue Sep 25, 2019
p-a-s-c-a-l added a commit that referenced this issue Sep 27, 2019
@p-a-s-c-a-l p-a-s-c-a-l added the BB: Scenario Management Scenario Management Building Block label Oct 28, 2019
@p-a-s-c-a-l p-a-s-c-a-l added this to To do in T4.3 Scenario Management via automation Oct 28, 2019
@p-a-s-c-a-l p-a-s-c-a-l moved this from To do to Done in T4.3 Scenario Management Nov 6, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
BB: Scenario Management Scenario Management Building Block enhancement New feature or request
Projects
No open projects
Development

No branches or pull requests

3 participants