Skip to content

Commit

Permalink
Merge pull request #125 from esmero/ISSUE-124
Browse files Browse the repository at this point in the history
ISSUE-124: Inherit right "defineDefaultBaseProperties" for LoD webform elements
  • Loading branch information
DiegoPino authored Jul 28, 2021
2 parents 64a0e1f + d39b07c commit 504e614
Show file tree
Hide file tree
Showing 7 changed files with 38 additions and 26 deletions.
31 changes: 23 additions & 8 deletions src/Controller/AuthAutocompleteController.php
Original file line number Diff line number Diff line change
Expand Up @@ -148,17 +148,22 @@ public static function create(ContainerInterface $container) {
*/
public function handleAutocomplete(Request $request, $auth_type, $vocab = 'subjects', $rdftype = NULL, $count) {
$results = [];


//@TODO pass count to the actual fetchers
//@TODO maybe refactor into plugins so others can write any other reconciliators
//@TODO if so, we can query the plugins and show in the webform builder options
// Get the typed string from the URL, if it exists.

$apikey = Settings::get('webform_strawberryfield.europeana_entity_apikey');
$input = $request->query->get('q');
if ($this->currentUser->isAnonymous()) {
sleep(5);
}
$csrf_token = $request->headers->get('X-CSRF-Token');
$is_internal = FALSE;

if (is_string($csrf_token)) {
$request_base = $request->getSchemeAndHttpHost().':'.$request->getPort();
$is_internal = $_SERVER['REQUEST_SCHEME'].'://'.$_SERVER['SERVER_ADDR'].':'.$_SERVER['SERVER_PORT'] == $request_base;
}

if ($input) {
$rdftype_str = $rdftype ?? 'null';
Expand All @@ -168,8 +173,12 @@ public function handleAutocomplete(Request $request, $auth_type, $vocab = 'subje
$cache_id = 'webform_strawberry:auth_lod:' . $cache_var;
$cached = $this->cacheGet($cache_id);
if ($cached) {
error_log('cached');
return new JsonResponse($cached->data);
}
if ($this->currentUser->isAnonymous() && !$is_internal) {
sleep(5);
}

switch ($auth_type) {
case 'loc':
Expand Down Expand Up @@ -202,10 +211,16 @@ public function handleAutocomplete(Request $request, $auth_type, $vocab = 'subje
}
// DO not cache NULL or FALSE. Those will be 401/403/500;
if ($results) {
//setting cache for
$this->cacheSet($cache_id, $results,
($this->time->getRequestTime() + static::MAX_CACHE_AGE),
['user:'.$this->currentUser->id()]);
//setting cache for anonymous or logged in
if (!$is_internal) {
$this->cacheSet($cache_id, $results,
($this->time->getRequestTime() + static::MAX_CACHE_AGE),
['user:' . $this->currentUser->id()]);
}
else {
// For internal calls. Where we have really no session or anything.
$this->cacheSet($cache_id, $results, ($this->time->getRequestTime() + static::MAX_CACHE_AGE));
}
}
$results = $results ?? [];
return new JsonResponse($results);
Expand Down Expand Up @@ -364,7 +379,7 @@ protected function wikidata($input) {
* @param string $vocab
*
* @param string $mode
* Can be either 'fuzzy' or 'subjects', exact for 1:1 preflalbe or combined
* Can be either 'fuzzy' or 'subjects', exact for 1:1 preflabel or combined
* subjects will be deprecated in 1.1
* @return array
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,7 @@ public function formElement(FieldItemListInterface $items, $delta, array $elemen
// Where is this field being used, a node?
$entity_type = $items->getEntity()->getEntityTypeId();
$bundle = $items->getEntity()->bundle();
$bundle_label = $items->getEntity()->type->entity->label();
$bundle_label = isset($items->getEntity()->type) ? $items->getEntity()->type->entity->label() : '';
$this_field_name = $this->fieldDefinition->getName();

// So does the current loaded entity, where this widget is shown
Expand Down Expand Up @@ -232,16 +232,17 @@ public function formElement(FieldItemListInterface $items, $delta, array $elemen
/** @var \Drupal\webform\WebformInterface $my_webform */
$my_webform = Webform::load($my_webform_machinename);
// Deals with any existing confirmation messages.
$confirmation_message = $my_webform->getSetting('confirmation_message', FALSE);
$confirmation_message = !empty($confirmation_message) && strlen(trim($confirmation_message)) > 0 ? $confirmation_message : $this->t(
'Thanks, you are all set! Please Save the content to persist the changes.');

if ($my_webform == NULL) {
// Well someone dropped the ball here
// and removed our super default
// or the original webform exists only in our hopes.
return $this->_exceptionElement($items, $delta, $element, $form, $form_state);
}
$confirmation_message = $my_webform->getSetting('confirmation_message', FALSE);
$confirmation_message = !empty($confirmation_message) && strlen(trim($confirmation_message)) > 0 ? $confirmation_message : $this->t(
'Thanks, you are all set! Please Save the content to persist the changes.');

$form_state->set('webform_machine_name', $my_webform_machinename);
try {
$form_state->set(
Expand Down Expand Up @@ -502,7 +503,7 @@ protected function _exceptionElement(FieldItemListInterface $items, $delta, arra
$element['strawberry_webform_widget']['creation_method'] = [
'#type' => 'value',
'#id' => 'webform_output_webform' . $form_state->get('strawberryfield_webform_widget_id'),
'#default_value' => $current_value['creation_method'],
'#default_value' => $current_value['creation_method'] ?? 'missing_webform',
];

return $element;
Expand Down
5 changes: 2 additions & 3 deletions src/Plugin/WebformElement/WebformEuropeana.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,15 +34,14 @@ protected function defineDefaultBaseProperties() {
return [
'vocab' => 'agent',
'rdftype' => 'thing',
];
] + parent::defineDefaultBaseProperties();
}

public function getDefaultProperties() {
$properties = parent::getDefaultProperties() + [
'vocab' => 'agent',
'rdftype' => 'thing',
] + parent::defineDefaultProperties()
+ $this->defineDefaultBaseProperties();
];

return $properties;
}
Expand Down
5 changes: 2 additions & 3 deletions src/Plugin/WebformElement/WebformGetty.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,15 +31,14 @@ protected function defineDefaultBaseProperties() {
return [
'vocab' => 'aat',
'matchtype' => 'fuzzy',
];
] + parent::defineDefaultBaseProperties();
}

public function getDefaultProperties() {
$properties = parent::getDefaultProperties() + [
'vocab' => 'aat',
'matchtype' => 'fuzzy',
] + parent::defineDefaultProperties()
+ $this->defineDefaultBaseProperties();
];

return $properties;
}
Expand Down
5 changes: 2 additions & 3 deletions src/Plugin/WebformElement/WebformLoC.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,15 +33,14 @@ protected function defineDefaultBaseProperties() {
return [
'vocab' => 'subjects',
'rdftype' => 'FullName',
];
] + parent::defineDefaultBaseProperties();
}

public function getDefaultProperties() {
$properties = parent::getDefaultProperties() + [
'vocab' => 'subjects',
'rdftype' => 'FullName',
] + parent::defineDefaultProperties()
+ $this->defineDefaultBaseProperties();
];

return $properties;
}
Expand Down
5 changes: 2 additions & 3 deletions src/Plugin/WebformElement/WebformMultiAgent.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ protected function defineDefaultBaseProperties() {
'vocab_corporate_name' => '',
'rdftype_corporate_name' => '',
'role_type' => '',
];
] + parent::defineDefaultBaseProperties();
}

public function getDefaultProperties() {
Expand All @@ -51,8 +51,7 @@ public function getDefaultProperties() {
'vocab_corporate_name' => '',
'rdftype_corporate_name' => '',
'role_type' => '',
]
+ parent::defineDefaultProperties();
];

unset($properties['multiple__header']);
return $properties;
Expand Down
2 changes: 1 addition & 1 deletion src/Plugin/WebformElement/WebformNominatim.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ public function getDefaultProperties() {
'geolocation' => FALSE,
'hidden' => FALSE,
'map' => FALSE,
] + $this->defineDefaultBaseProperties();
];
}

/**
Expand Down

0 comments on commit 504e614

Please sign in to comment.