You are here

public function FormatConverter::json_to_standard in Lingotek Translation 8

Converts source JSON into a StandardImportObject that can be imported into WP @author Unkown

Parameters

object $source_doc source document:

string $content JSON string that is then parsed to get the relevant: information for the title and body

Return value

StandardImportObject That can be imported into WP

File

src/Form/FormatConverter.php, line 79

Class

FormatConverter

Namespace

Drupal\lingotek\Form

Code

public function json_to_standard($source_doc, $content) {

  /**
   *These string replaces are to make it so that we can access the json objects
   *with these titles. They were being imported with @ before the key and that
   *made them inaccessible. This is specific to Middleware json strings.
   *@author Matt Smith and TJ Murphy
   */
  $content = str_replace('"@title"', '"title"', $content);
  $content = str_replace('"@type"', '"type"', $content);
  $decoded_json = json_decode($content);
  $content = t('We could not parse the data from this document. Are you sure that it is in a recognizable format, such as JSON, XML, HTML, or plain text?');
  $title = t('No title found');
  $error = false;

  /**
   *An if statement to check for the different json patterns to find the content
   *that will be the body of the WP imported post/page
   *@author Unkown and TJ Murphy
   *@return string $content is set -- if nothing matches then $content remains
   *with a warning
   *@see $content
   */
  if (isset($decoded_json->post->post_content)) {

    // Drupal 7
    $content = $decoded_json->post->post_content;
  }
  else {
    if (isset($decoded_json->body[0]->value)) {

      // Drupal 8
      $content = $decoded_json->body[0]->value;
    }
    else {
      if (isset($decoded_json->args->description)) {
        $content = $decoded_json->args->description;
      }
      else {
        if (isset($decoded_json->content->body)) {

          // Middleware - MindTouch
          $content = $decoded_json->content->body;
        }
        else {
          if (isset($decoded_json->body)) {

            // Middleware - Zendesk and HubSpot
            $content = $decoded_json->body;
          }
          else {
            if (isset($decoded_json->email_body)) {

              // Middleware - Email HubSpot
              $content = $decoded_json->email_body;
            }
            else {
              if (isset($decoded_json->post_summary)) {

                // Middleware
                $content = $decoded_json->post_summary;
              }
              else {
                if (isset($decoded_json->description)) {

                  // Middleware
                  $content = $decoded_json->description;
                }
              }
            }
          }
        }
      }
    }
  }

  /**
   *An if statement to check for the different json patterns to find the title
   *that will be the title of the WP imported post/page
   *@author Unkown and TJ Murphy
   *@return string $title is set -- if nothing matches then $error is set to TRUE
   *@see $title
   */
  if (isset($decoded_json->title[0]->value)) {

    // Drupal 8
    $title = $decoded_json->title[0]->value;
  }
  else {
    if (isset($decoded_json->post->post_title)) {

      // Drupal 7
      $title = $decoded_json->post->post_title;
    }
    else {
      if (isset($decoded_json->content->title)) {

        // Middleware - MindTouch
        $title = $decoded_json->content->title;
      }
      else {
        if (isset($decoded_json->title)) {

          // Middleware - Zendesk and HubSpot
          $title = $decoded_json->title;
        }
        else {
          if (isset($decoded_json->name)) {

            // Middleware - Email HubSpot
            $title = $decoded_json->name;
          }
          else {
            $error = true;
          }
        }
      }
    }
  }
  return new StandardImportObject($title, $content, $error);
}