You are here

function jquery_carousel_create_node in jQuery carousel 8

1 call to jquery_carousel_create_node()
jquery_carousel_examples_generate_content in jquery_carousel_examples/jquery_carousel_examples.install
Generate the sample content for views.

File

jquery_carousel_examples/jquery_carousel_examples.install, line 49
Adds dummy data for jquery carousel examples.

Code

function jquery_carousel_create_node($files, $image, $add_to_menu = FALSE) {

  // Create node object with attached file.
  $node = Node::create([
    'type' => 'jquery_carousel_example',
    'title' => ucfirst(str_replace('-', ' ', $image->name)),
    'promote' => NodeInterface::NOT_PROMOTED,
    'created' => \Drupal::time()
      ->getRequestTime(),
    'changed' => \Drupal::time()
      ->getRequestTime(),
    'uid' => 1,
    'field_jquery_carousel_image' => $files,
  ]);
  try {
    $result = $node
      ->save();
    if ($add_to_menu) {

      // Add a link to the page in jquery carousel example menu.
      MenuLinkContent::create([
        'title' => 'Multi-valued field widget Example',
        'link' => [
          'uri' => 'internal:/node/' . $node
            ->id(),
        ],
        'menu_name' => 'jquery-carousel-examples',
        'enabled' => 1,
      ])
        ->save();
    }
    if ($result == SAVED_NEW) {
      $saved = 'Created';
    }
    elseif ($result == SAVED_UPDATED) {
      $saved = 'Updated';
    }
    else {
      throw new Exception(t('Unknown value [@result] was returned', array(
        '@result' => $result,
      )));
    }
    \Drupal::logger('jquery_carousel_examples')
      ->info(t('@saved jquery_carousel_example node @nid', array(
      '@saved' => $saved,
      '@nid' => $node
        ->id(),
    )), array());
  } catch (Exception $e) {
    \Drupal::logger('jquery_carousel_examples')
      ->error(t('Node create failure: @msg', array(
      '@msg' => $e
        ->getMessage(),
    )), array());
  }
}