wsdata_field.module in Web Service Data 8
Same filename and directory in other branches
Main module file for wsdata_field.
File
modules/wsdata_field/wsdata_field.moduleView source
<?php
/**
* @file
* Main module file for wsdata_field.
*/
use Drupal\Core\Entity\EntityInterface;
use Drupal\Core\Entity\ContentEntityInterface;
use Drupal\Core\Url;
use Drupal\field_ui\FieldUI;
use Drupal\Core\Form\FormStateInterface;
/**
* Implements hook_entity_operation().
*/
function wsdata_field_entity_operation(EntityInterface $entity) {
// Check if this field has the custom stroage flag set to true.
if ('field_config' == $entity
->getEntityTypeId()) {
if ($entity
->getFieldStorageDefinition()
->hasCustomStorage()) {
// Also need to check see if there is a wsfield_config associated to it.
$wsfield_config = \Drupal::service('entity_type.manager')
->getStorage('wsfield_config')
->load($entity
->get('field_name'));
if ($wsfield_config != NULL) {
$operations = [];
$route_parameters = [
'field_config' => $entity
->id(),
] + FieldUI::getOverviewRouteInfo($entity
->get('entity_type'), $entity
->get('bundle'))
->getRouteParameters();
// Create the link to edit the wsfield configurations.
$operations['edit_wsfield'] = [
'title' => t('Web service configurations'),
'weight' => 50,
'url' => Url::fromRoute("entity.field_config.{$entity->getTargetEntityTypeId()}_wsfield_edit_form", $route_parameters),
'attributes' => [
'title' => t('Edit web service configurations'),
'class' => [
'dropbutton-action',
'web-service-config',
],
],
];
return $operations;
}
}
}
}
/**
* Implements hook_entity_delete().
*/
function wsdata_field_entity_delete(EntityInterface $entity) {
if ($entity
->getEntityTypeId() == 'field_storage_config' && $entity
->hasCustomStorage()) {
// It's a field storage config that has a custom storage.
$wsfield_config = \Drupal::service('entity_type.manager')
->getStorage('wsfield_config')
->load($entity
->get('field_name'));
if ($wsfield_config != NULL) {
// If there is a wsfield config we should delete it.
$wsfield_config
->delete();
}
}
}
/**
* Implements hook_entity_load().
*/
function wsdata_field_entity_load(array $entities, $entity_type_id) {
foreach ($entities as $entity) {
if ($entity instanceof ContentEntityInterface) {
// Fetch the field definitions for the this node.
$fields = $entity
->getFieldDefinitions();
foreach ($fields as $field) {
// Get the fields storage definitions.
$field_storage = $field
->getFieldStorageDefinition();
/* Check if the field has the custom storage flag set to true
and c heck to make sure the object is of type FieldStorageConfig. */
if ($field_storage
->hasCustomStorage()) {
if (is_a($field_storage, 'Drupal\\field\\Entity\\FieldStorageConfig')) {
// Fetch the wsfield config entity.
$wsfield_config = \Drupal::service('entity_type.manager')
->getStorage('wsfield_config')
->load($field_storage
->get('field_name'));
if ($wsfield_config != NULL) {
/* If the wsfield config exist we are in business
replace the value with the return of the wscall. */
$replacements = is_array($wsfield_config->replacements) ? $wsfield_config->replacements : [];
// Get the cache tags.
$tags = $entity
->getCacheTagsToInvalidate();
$wsdata = \Drupal::service('wsdata');
$langcode = $entity
->language()
->getId();
if ($wsfield_config->languageHandling && $wsfield_config->languageHandling == 'interfaceLanguage') {
$langcode = \Drupal::languageManager()
->getCurrentLanguage()
->getId();
}
$result = $wsdata
->call($wsfield_config->wscall, NULL, $replacements, $wsfield_config->data, [
'langcode' => $langcode,
], $wsfield_config->returnToken, [
$entity_type_id => $entity,
], $tags);
// Set the field with the wsdata results.
$entity
->set($field_storage
->get('field_name'), $result);
}
}
}
}
}
}
}
/**
* Implements hook_form_alter().
*/
function wsdata_field_form_alter(&$form, FormStateInterface $form_state, $form_id) {
if ('node_delete_multiple_confirm_form' !== $form_id && (preg_match('/^node_(.*)_edit_form/', $form_id) || preg_match('/^node_(.*)_form/', $form_id))) {
$entity = $form_state
->getFormObject()
->getEntity();
if ($entity
->getEntityTypeId() == 'node') {
$fields = $entity
->getFieldDefinitions();
foreach ($fields as $field) {
// Get the fields storage definitions.
$field_storage = $field
->getFieldStorageDefinition();
// Check if it has the custom storage flag set to true.
if ($field_storage
->hasCustomStorage()) {
// Check to make sure the object is of type FieldStorageConfig.
if (is_a($field_storage, 'Drupal\\field\\Entity\\FieldStorageConfig')) {
// Fetch the wsfield config entity.
$wsfield_config = \Drupal::service('entity_type.manager')
->getStorage('wsfield_config')
->load($field_storage
->get('field_name'));
if ($wsfield_config != NULL) {
// Make the field hidden by adding a class.
$form[$field_storage
->get('field_name')]['#attributes']['class'][] = 'hidden';
}
}
}
}
}
}
}
/**
* Implements hook_views_data_alter().
*/
function wsdata_field_views_data_alter(array &$data) {
$wsfield_configs = \Drupal::service('entity_type.manager')
->getStorage('wsfield_config')
->loadMultiple();
foreach ($wsfield_configs as $wsfield_config) {
// Load the field name to generate the views fields.
$field_name = $wsfield_config
->id();
$data['node'][$field_name] = [
'title' => $field_name,
'help' => t('WSdata field.'),
'field' => [
'title' => $field_name,
'id' => 'wsdata_field_views',
],
];
}
}
Functions
Name | Description |
---|---|
wsdata_field_entity_delete | Implements hook_entity_delete(). |
wsdata_field_entity_load | Implements hook_entity_load(). |
wsdata_field_entity_operation | Implements hook_entity_operation(). |
wsdata_field_form_alter | Implements hook_form_alter(). |
wsdata_field_views_data_alter | Implements hook_views_data_alter(). |