Friday, June 30, 2023

Site Information (TEST-Interview)



1. site_information/site_information.info.yml

name: Site Information

description: Custom module for add the field in site information form.

core: 8.x

package: Custom

type: module

version: 8.x-6.3


2. site_information / site_information.routing.yml

site_information.get_node_data:

  path: '/data/node_page/{id}'

  defaults:

    _controller: '\Drupal\site_information\Controller\SiteController::data'

    _title: 'node Data'

  requirements:

    _permission: 'access content'


3. site_information / site_information.module

<?php
use Drupal\Core\Form\FormStateInterface;
/**
 * Implements hook_form_system_site_information_settings_alter().
 */
function site_information_form_system_site_information_settings_alter(&$form, FormStateInterface $form_state, $form_id) {
  $site_api = \Drupal::config('api_key.settings')->get('site_api_key');
  $host = base_path();
  $form['site_information']['site_api_key'] = array(
    '#type' => 'textfield',
    '#title' => t('Site API Key'),
    '#default_value' => !empty($site_api) ? $site_api : t('No API key yet'),
    '#placeholder'=> t('No API key yet'),
  );
  if(!empty($site_api) && $site_api != 'No API key yet') {
  $value = t('Update Configuration');
  $path = $host . 'data/node_page/' . $site_api; 
  $form['site_information']['help'] = [
    '#type' => 'markup',
    '#markup' => '<a href="'. $path .'">'.$path . '</a>',
    '#weight' => '3',
];
  } 

 
   $form['actions']['submit']['#value'] = $value;
   $form['#validate'][] = 'my_users_form_site_information_form_validate';
   $form['#submit'][] = 'site_information_form_submit';
   return $form;
}
/**
 * Email submit callback for email_update_form.
 */
function my_users_form_site_information_form_validate(&$form, &$form_state) {
   $api_key = $form_state->getValue('site_api_key');
   $bundle = 'document';//content type name
   $query = \Drupal::entityQuery('node');
   $query->condition('status', 1);
   $query->condition('type', $bundle);
   $entity_ids = $query->execute();
   $arr2 = array("No API key yet");
   $entity_ids = array_merge($entity_ids, $arr2);
   if(!in_array($api_key, $entity_ids) && $api_key != '') {
    $form_state->setErrorByName('site_api_key', t('access denied.'));
   }
}

/**
 * submit callback for site information_form value.
 */
function site_information_form_submit(&$form, FormStateInterface &$form_state) {
// kint($form_state->getValue('site_api_key'));
// die();
$config = \Drupal::service('config.factory')->getEditable('api_key.settings');
  $config->set('site_api_key', $form_state->getValue('site_api_key'));
  $config->save();
  $message = "The Site API Key has been saved";
  drupal_set_message($message);
}

4. site_information / src / Controller / SiteController.php


namespace Drupal\site_information\Controller;

use Drupal\Core\Controller\ControllerBase;
use Drupal\node\Entity\Node;
use Symfony\Component\HttpFoundation\JsonResponse;

/**
 * Controller for export json.
 */
class SiteController extends ControllerBase {

  /**
   * {@inheritdoc}
   */
  public function data($id) {
    $json_array = array(
      'data' => array()
    );
    // print_r($id);
    // die();
    $nids = \Drupal::entityQuery('node')
    ->condition('type','document')//content type name
    ->execute();
    $nodes =  Node::loadMultiple($nids);
    foreach ($nodes as $node) {
      if($node->get('nid')->value == $id){
         $json_array['data'][] = array(
        'type' => $node->get('type')->target_id,
        'id' => $node->get('nid')->value,
        'attributes' => array(
          'title' =>  $node->get('title')->value,
          'content' => $node->get('body')->value,
        ),
      );
    }
     
    }
    return new JsonResponse($json_array);
  }
}

No comments:

Post a Comment

If you have any problem please let me know.