Unlink simple product variant from configurable product programmatically in magento 2

I want to unlink some (not all) simple products from configurable product.
I don’t want to delete simple products, I just want to unlink from the main configurable product programmatically.

search – Drupal 8: How to programmatically reindex commerce products?

I am using Drupal 8 Commerce module.

I am using Elastic Search on AWS platform.

From the Admin whenever product is saved, those changes are directly applied on Elastic Search server, but when I try with $product->save() it is not working 100%.

So if I have to update 10 products in Elastic Search how to achieve that programatically?

development – Is there a way to save a site in SharePoint online as a template programmatically (with an API, PowerShell, C#, etc.)?

I have a SharePoint Online site that has custom scripts enabled, and I want to programmatically save it as a template. I do not want to use PnP Provisioning Templates, I want to access the same functionality as savetmpl.aspx but programmatically. I know that in SharePoint Server you can use the SPWeb.SaveAsTemplate method in PowerShell or C#, but this is not present in SharePoint Online. Is there a way to do this?

I also found the following url: https://mytenant-admin.sharepoint.com/_vti_bin/sites.asmx. I see ExportWeb listed there. Would this work in SharePoint Online?

I looked at this question, but I do not want to use PnP Provisioning

7 – How to rename a content type programmatically

There are many answers and tutorials addressing the problem of changing the machine name of a content type, or migrating content types. However, I would like to rename a content type, without changing the machine name. This is what I tried:

$type = node_type_load('my_content_type');
$type->name = 'A new name';
node_type_save($type);

But this has no effect. What is the proper way to do this?

How do I programmatically send a mail after changing a workflow state?

How do I programmatically send a mail after changing a workflow state?

When a node is created, I send a mail. I want to send a mail on Workflow state operations.

function mails_node_insert(EntityInterface $entity) {
  if ($entity->getEntityTypeId() !== 'node' || ($entity->getEntityTypeId() === 'node' && $entity->bundle() !== 'article')) {
    return;
  }

  $node = Drupal::entityManager()->getStorage('node')->load($entity->id());

  if ($node->get("field_workflow")->value == "test_published") {
    $result = $node->get("body")->getString();
    if ($result != true) {
      drupal_set_message(t('Sorry , due to some error mail , we cant send a mail.'), 'error');
    }
    else {
      drupal_set_message(t('The mail has been sent successfully.'));
    }
  }
}

How to send a mail in Drupal 8 after changing a workflow state -programmatically?

How to send a mail in Drupal 8 after changing a workflow state with programming

On Node insert i send a mail like below, Now i wants to send a mail on Workflow state operations

function mails_node_insert(EntityInterface $entity) {

 if ($entity->getEntityTypeId() !== 'node' || ($entity->getEntityTypeId() === 'node' && $entity->bundle() !== 'article')) {
   return;
 }
 $node = Drupal::entityManager()->getStorage('node')->load($entity->id());

if ($node->get("field_workflow")->value == "test_published") // workflow state check on node insert
{
$send_mail = new DrupalCoreMailPluginMailPhpMail();
$from = 'test@xyz.com';
$to = 'test2@xyz.com';
$message('headers') = array(
        'content-type' => 'text/html',
        'MIME-Version' => '1.0',
       'reply-to' => $from,
       'from' => 'sender name <'.$from.'>'
);
$message('to') = $to;
$message('subject') = "Your article has been published";

$message('body') =$node->get("body")->getString();

$result=$send_mail->mail($message);

if ($result != true) {
   drupal_set_message(t('Sorry , due to some error mail , we cant send a mail.'), 'error');
 }
 else {
   drupal_set_message(t('The mail has been sent successfully.'));
 }
}
}

How to generate conditionals programmatically?

I need to generate conditionals (||, &&, etc) from a list conditionList that I don’t know beforehand how many/which elements will it contain.

For simplicity, let’s consider integers:

conditionList = {3,5};

So, taking this list, I’d like to generate the conditionals to filter some other list. For instance:

listToBeFiltered={1,2,3,4,5,6,7,8,9};

Select(listToBeFiltered, (# == 3 || # == 5) &)

Here I manually made the argument (# == 3 || # == 5) for Select. How could I have generated it from conditionList directly?

My first guess was to generate the conditions as strings, and then use ToExpression to apply it to Select, but this does not work:

myConditions = 
 StringDrop(
  StringJoin(
   Table(StringJoin("#==", IntegerString(conditionList((a))), 
     "||"), {a, 1, Length(conditionList)})), -2)

Gives:

#==3||#==5

Which when applied gives an empty list:

Select(listToBeFiltered, (ToExpression@myConditions) &)

{}

Thanks!

8 – How to programmatically print a webform block with caching enabled?

When displaying a Webform inside a normal block (using admin/structure/block) Drupal manage to correctly cache the page, the HTTP header displays X-Drupal-Dynamic-Cache: HIT.

However we need to display this webform inside a paragraph so we tried the following :

  • use twig_tweak module and {{ drupal_block('webform_...') }}
  • programmatically put the block in a template preprocess like this :
$my_form = DrupalwebformEntityWebform::load('contact_new');
$output = Drupal::entityManager()
          ->getViewBuilder('webform')
          ->view($my_form);
$variables('contact_form') = $output;

Both solutions seem to make the page uncacheable: X-Drupal-Dynamic-Cache: UNCACHEABLE.

What would be the correct way to put a block in a paragraph while make the page cacheable ?
How to mimic the standard block system to display a in our template ?

8 – How to create a term reference field programmatically

I need to add a taxonomy reference field to a node bundle, programmatically.

I found a couple examples here, but one actually is incorrectly titled and is about creating a from field for it, and the other is from 2012.

I have tried the following, based on the couple of suggested examples I found, and a further suggestion that one use the field/field storage export as a guide.

When it runs, it silently fails. The field is not created, nor is there an error thrown.

use DrupalCoreFieldBaseFieldDefinition;
use DrupalfieldEntityFieldStorageConfig;
use DrupalfieldEntityFieldConfig;

$handler_settings = (
      'target_bundles' => (
        '<vid>' => '<vid>'
      ),
      'sort' => (
        'field' => 'name',
        'direction' => 'asc',
      ),
      'auto_create' => FALSE,
      'auto_create_bundle' => '',
    );
    

$fields('<field_my_field>') = BaseFieldDefinition::create('entity_reference')
  ->setLabel(t('<My Field>'))
  ->setDescription(t(''))
  ->setRequired(TRUE)
  ->setSetting('target_type', 'taxonomy_term')
  ->setSetting('handler', 'default')
  ->setSetting('handler_settings', $handler_settings)
  ->setRevisionable(TRUE)
  ->setTranslatable(TRUE)
  ->setDisplayOptions('view', (
    'label' => 'hidden',
    'type' => '<vid>',
    'weight' => 0,
  ))
  ->setDisplayOptions('form', (
    'type' => 'entity_reference_autocomplete',
    'weight' => 5,
    'match_operator' => 'CONTAINS',
  ))
  ->setDisplayConfigurable('form', TRUE)
  ->setDisplayConfigurable('view', TRUE)
  ->setCardinality(1);