Skip to content

1175: Add action to generate events.xml file through the context menu #1189

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions resources/META-INF/plugin.xml
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@
<action id="MagentoCreateCrontabFile" class="com.magento.idea.magento2plugin.actions.context.xml.NewCrontabXmlAction"/>
<action id="MagentoCreateDiFile" class="com.magento.idea.magento2plugin.actions.context.xml.NewDiXmlAction"/>
<action id="MagentoCreateEmailTemplatesFile" class="com.magento.idea.magento2plugin.actions.context.xml.NewEmailTemplatesXmlAction"/>
<action id="MagentoCreateEventsFile" class="com.magento.idea.magento2plugin.actions.context.xml.NewEventsXmlAction"/>
<action id="MagentoCreateExtensionAttributesFile" class="com.magento.idea.magento2plugin.actions.context.xml.NewExtensionAttributesXmlAction"/>
<action id="MagentoCreateFieldsetFile" class="com.magento.idea.magento2plugin.actions.context.xml.NewFieldsetXmlAction"/>
<action id="MagentoCreateIndexerFile" class="com.magento.idea.magento2plugin.actions.context.xml.NewIndexerXmlAction"/>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
/*
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/

package com.magento.idea.magento2plugin.actions.context.xml;

import com.intellij.ide.fileTemplates.actions.AttributesDefaults;
import com.intellij.psi.PsiDirectory;
import com.intellij.psi.PsiFile;
import com.magento.idea.magento2plugin.actions.context.AbstractContextAction;
import com.magento.idea.magento2plugin.magento.files.ModuleEventsXml;
import com.magento.idea.magento2plugin.magento.packages.Areas;
import com.magento.idea.magento2plugin.magento.packages.ComponentType;
import com.magento.idea.magento2plugin.magento.packages.Package;
import com.magento.idea.magento2plugin.util.magento.GetMagentoModuleUtil;
import java.util.Arrays;
import java.util.List;
import org.jetbrains.annotations.NotNull;

public class NewEventsXmlAction extends AbstractContextAction {

public static final String ACTION_NAME = "Magento 2 Events File";
public static final String ACTION_DESCRIPTION = "Create a new Magento 2 events.xml file";

/**
* New events.xml file generation action constructor.
*/
public NewEventsXmlAction() {
super(ACTION_NAME, ACTION_DESCRIPTION, new ModuleEventsXml());
}

@Override
protected boolean isVisible(
final @NotNull GetMagentoModuleUtil.MagentoModuleData moduleData,
final PsiDirectory targetDirectory,
final PsiFile targetFile
) {
final PsiDirectory configDir = moduleData.getConfigDir();
final PsiDirectory globalScopeDir = getGlobalScopeDir(targetDirectory);

if (configDir == null || globalScopeDir == null) {
return false;
}

final List<String> allowedDirectories = Arrays.asList(
Package.moduleBaseAreaDir,
Areas.adminhtml.toString(),
Areas.crontab.toString(),
Areas.frontend.toString(),
Areas.graphql.toString(),
Areas.webapi_rest.toString(),
Areas.webapi_soap.toString()
);

return allowedDirectories.contains(targetDirectory.getName())
&& globalScopeDir.equals(configDir)
&& moduleData.getType().equals(ComponentType.module);
}

@Override
protected AttributesDefaults getProperties(
final @NotNull AttributesDefaults defaults,
final @NotNull GetMagentoModuleUtil.MagentoModuleData moduleData,
final PsiDirectory targetDirectory,
final PsiFile targetFile
) {
return defaults;
}
}